讀書筆記(重構(gòu)_改善既有代碼設(shè)計_第八章重新組織數(shù)據(jù))
Posted on 2009-08-18 21:02 在從未放棄的路上 閱讀(230) 評論(0) 編輯 收藏 所屬分類: 設(shè)計模式1.Self Encapsulate Field
為這個field建立getting/setting method,并且只通過這些函數(shù)來訪問field.
例:
privte int low,high;
boolean includes(int arg){
return arg>=low&&arg<=high;
}
重構(gòu)為:
private int low,high;
boolean includes(int arg){
return arg>=getLow()&&arg<=getHigh();
}
int getLow(){return low;}
int getHigh(){return high;}
將一個數(shù)據(jù)項變成一個對象。
3.Change Value to Reference
將這個實值對象變成一個引用對象。
4.Change Reference to Value
將一個引用對象變成實值對象。
例:
Class Currency{
private String code;
public String getCode()}
return code;
}
private Currency(String code){
this.code=code;
}
}
5.Replace Array with Object
以對象替換數(shù)組,對于數(shù)組中的每個元素,以一個值域表示之。
例:
String[] row=new String[3];
row[0]="Liverpool";
row[1]="15";
重構(gòu)為:
Performance row=new Performance();
row.setName("Liverpool");
row.setWins("15");
6.Duplicate Observed Data
7.Change Unidirectional Association to Bidirectional
8.Change Bidirectional Association to Unidirectional