惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
U
Unit 42
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
I
InfoQ
WordPress大学
WordPress大学
H
Help Net Security
D
Docker
B
Blog
腾讯CDC
A
About on SuperTechFans
Recent Announcements
Recent Announcements
雷峰网
雷峰网
有赞技术团队
有赞技术团队
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 网络金领

C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll] DataTable的数据批量写入数据库 C# .NET锁屏程序(顺带屏蔽任务管理器) 步步为营 .NET 代码重构学习 十一 步步为营 .NET 代码重构学习笔记 九 步步为营 .NET 代码重构学习笔记 八 步步为营 .NET 代码重构学习笔记 七 步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field) 步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm) 步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable) 步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method) 步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method) 步步为营 .NET 代码重构学习笔记 一、为何要代码重构 步步为营 .NET 设计模式学习笔记 二十四、Factory Method(工厂方法模式) 步步为营 .NET 设计模式学习笔记 二十三、Interpreter(解释器模式) 步步为营 .NET 设计模式学习笔记 二十二、Memento(备望录模式) 步步为营 .NET 设计模式学习笔记 二十一、Visitor(访问者模式) 步步为营 .NET 设计模式学习笔记 二十、Mediator(中介者模式) 步步为营 .NET 设计模式学习笔记 十九、Chain of Responsibility(职责链模式)
步步为营 .NET 代码重构学习笔记 十
网络金领 · 2011-06-02 · via 博客园 - 网络金领

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

动机(Motivation)

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

示例

03     private int _type;
04     public static int ENGINEER = 0;
05     public static int SALEMAN = 1;
06     public static int MANAGER = 2;
08     public Employee(int type)

改为

03     private int _type;
04     public static int ENGINEER = 0;
05     public static int SALEMAN = 1;
06     public static int MANAGER = 2;
08     public Employee(int type)
15         get { return _type; }
16         set { _type = value; }
22 public class ENGINEER : Employee
24     public int GetType()
26         return Employee.ENGINEER;
30 public class SALEMAN : Employee
32     public int GetType()
34         return Employee.SALEMAN;
38 public class MANAGER : Employee
40     public int GetType()
42         return Employee.MANAGER;
46 public class Factory:Employee
48     public Employee Create(int type)
52             case ENGINEER:
53                 return new ENGINEER();
54             case SALEMAN:
55                 return new SALEMAN();
56             case MANAGER:
57                 return new MANAGER();
59                 throw new ExecutionEngineException("Incorrect type code value");

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

动机(Motivation)

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

示例

03     private int _type;
04     public static int ENGINEER = 0;
05     public static int SALEMAN = 1;
06     public static int MANAGER = 2;
08     public Employee(int type)
15         get { return _type; }
16         set { _type = value; }
19     public int PayAmount()
21         switch (_type)
23             case ENGINEER:
24                 return 100;
25             case SALEMAN:
26                 return 1000;
27             case MANAGER:
28                 return 10000;
30                 throw new ExecutionEngineException("Incorrect type code value");

改为

03     private int _type;
04     public static int ENGINEER = 0;
05     public static int SALEMAN = 1;
06     public static int MANAGER = 2;
08     public Employee(int type)
15         get { return _type; }
16         set { _type = value; }
19     public int PayAmount()
21         EmployeeType employeeType;
22         switch (_type)
24             case ENGINEER:
25                 employeeType= new ENGINEER();
26                 break;
27             case SALEMAN:
28                 employeeType=new SALEMAN();
29                 break;
30             case MANAGER:
31                 employeeType = new MANAGER();
32                 break;
34                 throw new ExecutionEngineException("Incorrect type code value");
36         return employeeType.GetType();
41 public class ENGINEER : EmployeeType
43     public override  int GetType()
49 public class SALEMAN : EmployeeType
51     public override int GetType()
57 public class MANAGER : EmployeeType
59     public override int GetType()
65 public abstract class EmployeeType
67     public abstract int GetType();

三、Replace Subclass with Fields(以值域取代子类)

动机(Motivation)

修改这些函数,使它们返回superclass中的某个(新增值域,然后销毁subclasses)

示例

01 public abstract class Person
03    public  abstract bool IsMale();
04    public  abstract string GetCode();
06    public Person CreateMale()
08        return new  Male();
11    public Person CreateFemale()
13        return new Female();
17 public class Male : Person
20     public override bool IsMale()
25     public override string GetCode()
31 public class Female : Person
34     public override bool IsMale()
39     public override string GetCode()

改为

03     private bool _IsMale;
04     private string _Code;
06     public bool IsMale
08         get { return _IsMale; }
09         set { _IsMale = value; }
12     public string Code
14         get { return _Code; }
15         set { _Code = value; }
17     public Person(bool isMale, string code)
19         this._IsMale = isMale;
20         this._Code = code;
24 public class Male : Person
28         : base(true, "M")
32 public class Female : Person
35         : base(false, "F")

四、Decompose Conditional(分解条件式)

动机(Motivation)

从if、then、else三个段落中分别提炼出独立函数。

示例

1 if(date<SUMMER_START||date>SUMMER_BND)
2    charge=quantity*_winterRate+_winterServiceCharge;
4     charge=quantity*_summerRate;

改为

2    charge=winterCharge(quantity);
4     charge=summerCharge(quantity);

五、Consolidate Conditional Expression(合并条件式)

动机(Motivation)

将很多条件合并成一个条件式,并将这个条件式提炼成为一个独立函数。

示例

1 public double DisabilityAmount()
3     if (_seniority < 2) return 0;
4     if (_monthsDisabled > 12) return 0;
5     if (_isPartTime) return 0;

改为

1 public double DisabilityAmount()
3     if (IsNotBligableForDisability()) return 0;

六、Consolidate Duplicate Conditional Fragments(合并重复的条件片段)

动机(Motivation)

将重复代码搬移到条件式之外。

示例

03     total = price * 0.95;
08     total = price * 0.98;

改为

2     total = price * 0.95;
4     total = price * 0.98;