Encapsulate Field

From CSSEMediaWiki
Revision as of 06:53, 6 October 2008 by Johannes Pagwiwoko (Talk | contribs)
Jump to: navigation, search

This is a common refactoring method suggested by Martin Fowler's in his Refactoring book. Encapsulate Field states that we should Replace public fields (instance variables) with private fields and provide public accessors.

For instance, if we have a particular code/class like this:

 public class Person {
   public String name;
 }

After we refactor the class using Encapsulate Field, we will have:

 public class Person {
   private String name;
   
   public void setName(String newName) {
       name = newName;
   }
   
   public String getName() {
       return name;
   }
 }
Personal tools