Refactoring-Improving the Design of Existing Code——学习笔记(3)
Chapter8 Organizing Data
(1)SelfEncapsulate Field
You areaccessing a field directly, but the coupling to the field is becoming awkward.Creating getting and setting methods and use only those to access the field.
private int _low,_high;
Boolean includes(int arg) {
return arg <= _low&& arg <=_high;
}
重构后:
private int _low,_high;
Boolean includes(int arg) {
return arg >= getLow()&&arg<=getHigh();
}
int getLow() {return _low;}
int getHigh() {return _high;}
(2)Replace Data Value with Object
You have a dataitem that needs additional data or behavior. Turn the data item into an object.

(3)Change Value to Reference
You have a classwith many equal instances that you want to replace with a single object. Turnthe object into a reference object.

(4)Change Reference to Value
You have areference object that is small, immutable, and awkward to manage. Turn it intoa value object.

(5)Replace Array with Object
You have anarray in which certain elements mean difference things. Replace the array withan object that has a field for each element.
String[] row = new String[3];
row[0] = “Liverpool”;
row[1] = “15”;
重构后:
Performance row = new Performance();
row.setName(“Liverpool”);
row.setWins(“15”);
(6)Duplicate Observed Data
You have domaindata available only in a GUI control, and domain methods need access. Copy thedata to a domain object. Set up an observer to synchronize the two pieces ofdata.

(7)Change Unidirectional Association to Bidirectional
You have twoclasses that need to use each other’s features, but there is only a one-waylink. Add back pointers, and change modifiers to update both sets.

(8)Replace Magic Number with Symbolic Constant
You have aliteral number with a particular meaning. Create a constant, name it after themeaning, and replace the number with it.
double potentialEnergy(double mass, double height) {
return mass * 9.81 *height
}
重构后:
double potentialEnergy(double mass, double height) {
return mass * GRAVITATIONAL_CONSTANT * height;
}
static final double GRAVITATIONAL_CONSTANT = 9.81;
思考:对于变量最好能够取一个相对好的名字,提高可读性。
(9)Encapsulate Field
There is apublic field. Make it private and provide accessors.
public String _name;
重构后:
private String _name;
public String getName() {return _name;}
public void setName(String arg) {_name=arg;}
(10)Encapsulate Collection
A method returnsa collection. Make it return a read only view and provide add/remove methods.

(11)Replace Record with Data Class
You need tointerface with a record structure in a traditional programming environment.Make a dumb data object for the record.
(12)Replace Type Code with Classs
A class has anumeric type code that does not affect its behavior. Replace the number withnew class.

(13)Replace Type Code with Subclasses
You have animmutable type code that affects the behavior of a class. Replace the type codewith subclasses.

(14)Replace Type Code with State/Strategy
You have a typecode that affects the behavior of a class, but you cannot use subclassing.Replace the type code with a state object.

(15)Replace Subclass with Fields
You havesubclasses that vary only in methods that return constant data. Change themethods to superclass fields and eliminate the subclasses.
