一、Duplicate Observed Data(复制[被监视数据])
动机(Motivation)
将该笔数据拷贝到一个domain object中。建立一个Observer模式,用以对domain object 和GUI object内的重复数据进行同步控制(sync)。
示例
步步为营 .NET 设计模式学习笔记 十二、Observer (观察者模式)
二、Change Unidirectional Association to Bidirectional(将单向关联改为双向)
动机(Motivation)
添加一个反指针,并使修改函数(modifiers)能够同时更新两条连接。
示例
03 |
private Customer _customer; |
05 |
public Customer NewCustomer |
07 |
get { return _customer; } |
08 |
set { _customer = value; } |
改为
03 |
public List<Customer> CustomerList = new List<Customer>(); |
05 |
public void AddCustomer(Customer arg) |
07 |
arg.orderlist.Add( this ); |
08 |
CustomerList.Add(arg); |
11 |
public void RemoveCustomer(Customer arg) |
13 |
arg.orderlist.Remove( this ); |
14 |
CustomerList.Remove(arg); |
20 |
public List<Order> orderlist = new List<Order>(); |
21 |
public void AddOrder(Order arg) |
23 |
arg.CustomerList.Add( this ); |
26 |
public void RemoveOrder(Order arg) |
28 |
arg.CustomerList.Remove( this ); |
29 |
orderlist.Remove(arg); |
三、Change Bidirectional Association to Unidirectional(将双向改为单向)
动机(Motivation)
两个classes之间有双向关联,但其中一个class如今不再需要另一个class的特性。
示例
03 |
public List<Customer> CustomerList = new List<Customer>(); |
05 |
public void AddCustomer(Customer arg) |
07 |
arg.orderlist.Add( this ); |
08 |
CustomerList.Add(arg); |
11 |
public void RemoveCustomer(Customer arg) |
13 |
arg.orderlist.Remove( this ); |
14 |
CustomerList.Remove(arg); |
20 |
public List<Order> orderlist = new List<Order>(); |
21 |
public void AddOrder(Order arg) |
23 |
arg.CustomerList.Add( this ); |
26 |
public void RemoveOrder(Order arg) |
28 |
arg.CustomerList.Remove( this ); |
29 |
orderlist.Remove(arg); |
改为
03 |
private Customer _customer; |
05 |
public Customer NewCustomer |
07 |
get { return _customer; } |
08 |
set { _customer = value; } |
四、Replace Magic Number with Symbolic Constant(以符号常量/字面常量取侙魔法数)
动机(Motivation)
创造一个常量,根据其意义为它命名,并将上述的字面数值替换为这个常量。
示例
1 |
public double PotentialEnergy( double mass, double height) |
3 |
return mass * 9.51 * height; |
改为
1 |
private double GRAVITATIONAL_CONSTANT = 9.51; |
2 |
public double PotentialEnergy( double mass, double height) |
4 |
return mass * GRAVITATIONAL_CONSTANT * height; |
五、Encapsulate Field(封装值域)
动机(Motivation)
将它声明为private,并提供相应的访问函数(accessors)
示例
改为
六、Replace Type Code with Class(以类取代型别码)
动机(Motivation)
以一个新的class替换该数值型别码(type code)
示例
08 |
private int _bloodGroup; |
10 |
public Person( int bloodGroup) |
12 |
_bloodGroup = bloodGroup; |
14 |
public int NewBloodGroup |
16 |
get { return _bloodGroup; } |
17 |
set { _bloodGroup = value; } |
改为
03 |
public int O = BloodGroup.O.Code; |
04 |
public int A = BloodGroup.A.Code; |
05 |
public int B = BloodGroup.B.Code; |
06 |
public int AB = BloodGroup.AB.Code; |
09 |
private int _bloodGroup; |
11 |
public Person( int bloodGroup) |
13 |
_bloodGroup = bloodGroup; |
16 |
public int NewBloodGroup |
18 |
get { return _bloodGroup; } |
19 |
set { _bloodGroup = value; } |
23 |
public class BloodGroup |
25 |
public static BloodGroup O = new BloodGroup(0); |
26 |
public static BloodGroup A = new BloodGroup(1); |
27 |
public static BloodGroup B = new BloodGroup(2); |
28 |
public static BloodGroup AB = new BloodGroup(3); |
32 |
private BloodGroup( int code) |
40 |
set { _code = value; } |