步步为营 .NET 代码重构学习笔记 七

一、Extract Class(提炼类)

动机(Motivation)

一个class应该是一个清楚的抽象(abstract),处理一些明确的责任。但在实际工作中,class会不断成长扩展。这个class会变得过份复杂,很快,class就会变成一团乱麻。

示例

01 public class Person
02 {
03     private string _name;
04     private string _officeAreaCode;
05     private string _officeNumber;
06  
07     public string Name
08     {
09         get { return _name; }
10         set { _name = value; }
11     }
12  
13     public string OfficeAreaCode
14     {
15         get { return _officeAreaCode; }
16         set { _officeAreaCode = value; }
17     }
18  
19     public string OfficeNumber
20     {
21         get { return _officeNumber; }
22         set { _officeNumber = value; }
23     }
24  
25     public string TelephoneNumber
26     {
27         get { return "(" + _officeAreaCode + ")" + _officeNumber; }
28     }
29 }

改为

01 public class Person
02   {
03       private string _name;
04       private Telephone _telephoneNumber;
05  
06       public Telephone TelephoneNumber
07       {
08           get { return _telephoneNumber; }
09           set { _telephoneNumber = value; }
10       }
11  
12       public string Name
13       {
14           get { return _name; }
15           set { _name = value; }
16       }
17  
18       public string TemphoneNumber
19       {
20           get { return _telephoneNumber.TelephoneNumber; }
21       }
22  
23   }
24   public class Telephone
25   {
26       private string _officeAreaCode;
27       private string _officeNumber;
28       public string OfficeAreaCode
29       {
30           get { return _officeAreaCode; }
31           set { _officeAreaCode = value; }
32       }
33  
34       public string OfficeNumber
35       {
36           get { return _officeNumber; }
37           set { _officeNumber = value; }
38       }
39  
40       public string TelephoneNumber
41       {
42           get { return "(" + _officeAreaCode + ")" + _officeNumber; }
43       }
44   }

二、Inline Class(将类内联化)

动机(Motivation)

一个class不再承担足够责任、不再有单独存在的理由,将class的所有特性搬移到另一个class中,然后移除原class。

示例

01 public class User
02 {
03     private int _ID;
04     private string _name;
05  
06     public int ID
07     {
08         get { return _ID; }
09         set { _ID = value; }
10     }
11  
12     public string Name
13     {
14         get { return _name; }
15         set { _name = value; }
16     }
17  
18  
19 }
20 public class UserInfo
21 {
22     private int _ID;
23     private string _Age;
24  
25     public string ID
26     {
27         get { return _ID; }
28         set { _ID = value; }
29     }
30  
31     public string Age
32     {
33         get { return _Age; }
34         set { _Age = value; }
35     }
36 }

改为

01 public class User
02 {
03     private int _ID;
04     private string _name;
05     private string _Age;
06  
07     public int ID
08     {
09         get { return _ID; }
10         set { _ID = value; }
11     }
12  
13     public string Name
14     {
15         get { return _name; }
16         set { _name = value; }
17     }
18  
19     public string Age
20     {
21         get { return _Age; }
22         set { _Age = value; }
23     }
24 }

三、Hide Delegate(隐藏[委托关系])

动机(Motivation)

[封装]即使不是对象的最关键特征,也是最关键特征之一。[封装]意味每个对象都应该尽可能少了解系统的其它部分。如此一来,一旦发生变化,需要了解这变化的对象就会比较少,这会使谈化比较容易进行。

示例

01 public class User
02 {
03     private int _ID;
04     private string _name;
05     private Department _depart;
06  
07     public int ID
08     {
09         get { return _ID; }
10         set { _ID = value; }
11     }
12  
13     public string Name
14     {
15         get { return _name; }
16         set { _name = value; }
17     }
18  
19     public Department Depart
20     {
21         get { return _depart; }
22         set { _depart = value; }
23     }
24  
25 }
26  
27 public class Department
28 {
29     private string _departName;
30     private User _manager;
31  
32     public string DepartName
33     {
34         get { return _departName; }
35         set { _departName = value; }
36     }
37  
38     public User Manager
39     {
40         get { return _manager; }
41         set { _manager = value; }
42     }
43 }

改为

01 public class User
02   {
03       private int _ID;
04       private string _name;
05       private Department _depart;
06  
07       public int ID
08       {
09           get { return _ID; }
10           set { _ID = value; }
11       }
12  
13       public string Name
14       {
15           get { return _name; }
16           set { _name = value; }
17       }
18  
19       public Department Depart
20       {
21           get { return _depart; }
22           set { _depart = value; }
23       }
24  
25       public string DepartmentManager
26       {
27           get { return _depart.Manager; }
28       }
29   }
30  
31   public class Department
32   {
33       private string _departName;
34       private User _manager;
35  
36       public string DepartName
37       {
38           get { return _departName; }
39           set { _departName = value; }
40       }
41  
42       public User Manager
43       {
44           get { return _manager; }
45           set { _manager = value; }
46       }
47   }

这样取部门管理者就更直接,更方便,适合于少量使用。

但如果有大量的方法都用到了DepartmentManager里,我们就要去掉中间人了。

四、Remove Middle Man(移除中间人)

动机(Motivation)

大量重复的代码是万恶之源。这些重复性代码应该抽出来放进同一个函数中。

示例

01 public class User
02 {
03     private int _ID;
04     private string _name;
05     private Department _depart;
06  
07     public int ID
08     {
09         get { return _ID; }
10         set { _ID = value; }
11     }
12  
13     public string Name
14     {
15         get { return _name; }
16         set { _name = value; }
17     }
18  
19     public Department Depart
20     {
21         get { return _depart; }
22         set { _depart = value; }
23     }
24  
25     public string DepartmentManager
26     {
27         get { return _depart.Manager; }
28     }
29 }
30  
31 public class Department
32 {
33     private string _departName;
34     private User _manager;
35  
36     public string DepartName
37     {
38         get { return _departName; }
39         set { _departName = value; }
40     }
41  
42     public User Manager
43     {
44         get { return _manager; }
45         set { _manager = value; }
46     }
47 }

改为

01 public class User
02 {
03     private int _ID;
04     private string _name;
05     private Department _depart;
06  
07     public int ID
08     {
09         get { return _ID; }
10         set { _ID = value; }
11     }
12  
13     public string Name
14     {
15         get { return _name; }
16         set { _name = value; }
17     }
18  
19     public Department Depart
20     {
21         get { return _depart; }
22         set { _depart = value; }
23     }
24  
25 }
26  
27 public class Department
28 {
29     private string _departName;
30     private User _manager;
31  
32     public string DepartName
33     {
34         get { return _departName; }
35         set { _departName = value; }
36     }
37  
38     public User Manager
39     {
40         get { return _manager; }
41         set { _manager = value; }
42     }
43 }

在调用时就直接Depart.Manager来取得它的值.

posted @ 2011-06-02 00:21  网络金领  阅读(275)  评论(0编辑  收藏  举报