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