首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

Refactoring-Improving the Design of Existing Code——学习札记(3)

2012-06-20 
Refactoring-Improving the Design of Existing Code——学习笔记(3)Chapter8 Organizing Data(1)SelfEncaps

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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(4)Change Reference to Value

You have areference object that is small, immutable, and awkward to manage. Turn it intoa value object.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(13)Replace Type Code with Subclasses

You have animmutable type code that affects the behavior of a class. Replace the type codewith subclasses.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

(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.

Refactoring-Improving the Design of Existing Code——学习札记(3)

 

热点排行