一、Replace Type Code with Subclasses (以子类取代型别码)
动机(Motivation)
以一个subclass取代这个type code,如果面对的type code不会影响宿主类的行为,可以使用Replace Type Code with Class 来处理它们。但如果type code 会影响宿主类的行为,那么最好的办法就是借助多态(polymorphism)业处理 变化行为。
示例
04 |
public static int ENGINEER = 0; |
05 |
public static int SALEMAN = 1; |
06 |
public static int MANAGER = 2; |
08 |
public Employee(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) |
16 |
set { _type = value; } |
22 |
public class ENGINEER : Employee |
26 |
return Employee.ENGINEER; |
30 |
public class SALEMAN : Employee |
34 |
return Employee.SALEMAN; |
38 |
public class MANAGER : Employee |
42 |
return Employee.MANAGER; |
46 |
public class Factory:Employee |
48 |
public Employee Create(int type) |
53 |
return new ENGINEER(); |
59 |
throw new ExecutionEngineException("Incorrect type code value"); |
二、Replace Type Code with State/Strategy(以State/Strategy取代型别码)
动机(Motivation)
以State object(专门用来描述状态的对象)取代type code。
示例
04 |
public static int ENGINEER = 0; |
05 |
public static int SALEMAN = 1; |
06 |
public static int MANAGER = 2; |
08 |
public Employee(int type) |
16 |
set { _type = value; } |
19 |
public int PayAmount() |
30 |
throw new ExecutionEngineException("Incorrect type code value"); |
改为
04 |
public static int ENGINEER = 0; |
05 |
public static int SALEMAN = 1; |
06 |
public static int MANAGER = 2; |
08 |
public Employee(int type) |
16 |
set { _type = value; } |
19 |
public int PayAmount() |
21 |
EmployeeType employeeType; |
25 |
employeeType= new ENGINEER(); |
28 |
employeeType=new SALEMAN(); |
31 |
employeeType = new MANAGER(); |
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() |
11 |
public Person CreateFemale() |
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() |
改为
08 |
get { return _IsMale; } |
09 |
set { _IsMale = value; } |
15 |
set { _Code = value; } |
17 |
public Person(bool isMale, string code) |
19 |
this._IsMale = isMale; |
24 |
public class Male : Person |
32 |
public class Female : Person |
四、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)
将重复代码搬移到条件式之外。
示例
改为