当前位置:编程学习 > asp >>

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

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

html">步步为营 .NET 代码重构学习笔记 一、为何要代码重构

步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method)

步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method)

步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable)

步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm)

步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field)

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

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

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

一、Replace Type Code with Subclasses (以子类取代型别码)

动机(Motivation)

以一个subclass取代这个type code,如果面对的type code不会影响宿主类的行为,可以使用Replace Type Code with Class 来处理它们。但如果type code 会影响宿主类的行为,那么最好的办法就是借助多态(polymorphism)业处理 变化行为。

示例

view sourceprint?01 public class Employee 

02 { 

03     private int _type; 

04     public static int ENGINEER = 0; 

05     public static int SALEMAN = 1; 

06     public static int MANAGER = 2; 

07  

08     public Employee(int type) 

09     { 

10         _type = type; 

11     } 

12 }

 

改为

view sourceprint?01 public class Employee 

02 { 

03     private int _type; 

04     public static int ENGINEER = 0; 

05     public static int SALEMAN = 1; 

06     public static int MANAGER = 2; 

07  

08     public Employee(int type) 

09     { 

10         _type = type; 

11     } 

12  

13     public int Type 

14     { 

15         get { return _type; } 

16         set { _type = value; } 

17     } 

18  

19  

20 } 

21  

22 public class ENGINEER : Employee 

23 { 

24     public int GetType() 

25     { 

26         return Employee.ENGINEER; 

27     } 

28 } 

29  

30 public class SALEMAN : Employee 

31 { 

32     public int GetType() 

33     { 

34         return Employee.SALEMAN; 

35     } 

36 } 

37  

38 public class MANAGER : Employee 

39 { 

40     public int GetType() 

41     { 

42         return Employee.MANAGER; 

43     } 

44 } 

45  

46 public class Factory:Employee 

47 { 

48     public Employee Create(int type) 

49     { 

50         switch (type) 

51         { 

52             case ENGINEER: 

53                 return new ENGINEER(); 

54             case SALEMAN: 

55                 return new SALEMAN(); 

56             case MANAGER: 

57                 return new MANAGER(); 

58             default: 

59                 throw new ExecutionEngineException("Incorrect type code value"); 

60         } 

61     } 

62 }

 

二、Replace Type Code with State/Strategy(以State/Strategy取代型别码)

动机(Motivation)

以State object(专门用来描述状态的对象)取代type code。

示例

view sourceprint?01 public class Employee 

02 { 

03     private int _type; 

04     public static int ENGINEER = 0; 

05     public static int SALEMAN = 1; 

06     public static int MANAGER = 2; 

07  

08     public Employee(int type) 

09     { 

10         _type = type; 

11     } 

12  

13     public int Type 

14     { 

15         get { return _type; } 

16         set { _type = value; } 

17     } 

18  

19     public int PayAmount() 

20     { 

21         switch (_type) 

22         { 

23             case ENGINEER: 

24                 return 100; 

25             case SALEMAN: 

26                 return 1000; 

27     &n

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,