《重构—改善既有代码的设计》手法汇总

4,078
原文链接: www.cnblogs.com

重构手法

代码的坏味道

以下内容来自《重构—改善既有代码的设计》

 

一、重新组织函数

1、Extract Method (提炼函数)

2、 Inline Method (内联函数)

3、Inline Temp (内联临时变量)

4、Replace Temp with Query (以查询取代临时变量)

5、Introduce Explaining Variable (引入解释性变量)

6、Split Temporary Variable (分解临时变量)

7、Remove Assignments to Parameters (移除对参数的赋值)

8、Replace Method with Method Object (以函数对象取代函数)

9、Substitute Algorithm (替换算法)

二、在对象之间搬移特性

10、Move Method (搬移函数)

11、Move Field (搬移字段)

12、Extract Class (提炼类)

13、Inline Class (将类内联化)

14、Hide Delegate (隐藏委托关系)

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

16、Introduce Foreign Method (引入外加函数)

17、Introduce Local Extension (引入本地扩展)

三、重新组织数据

18、Self Encapsulate Field (自封装字段)

19、Replace Data Value with Object (以对象取代数据值)

20、Change Value to Reference (将值对象改为引用对象)

21、Change Reference to Value (将引用对象改为值对象)

22、Replace Array with Object (以对象取代数组)

23、Duplicate Observed Data (复制“被监视数据”)

24、Change Unidirectional Association to Bidirectional (将单向关联改为双向关联)

25、Change Bidirectional Association to Unidirectional (将双向关联改为单向关联)

26、Replace Magic Number with SymBolic Constant (以字面常量取代魔法数)

27、Encapsulated Field (封装字段)

28、Encapsulated Collection (封装集合)

29、Replace Record with Data Class (以数据类取代记录)

30、Replace Type Code with Class (以类取代类型码)

31、Replace Type Code with Subclass (以子类取代类型码)

32、Replace Type Code with State/Strategy (以状态/策略取代类型码)

33、Replace Subclass with Field (以字段取代子类)

四、简化条件表达式

34、Decompose Conditional (分解条件表达式)

35、Consolidate Conditional Expression (合并条件表达式)

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

37、Remove Control Flag (移除控制标记)

38、Replace Nested Conditional with Guard Clauses (以卫语句取代嵌套条件表达式)

39、Replace Conditional with Polymorphism (以多态取代条件表达式)

40、Introduce Null Object (引入Null 对象)

41、Introduce Assertion (引入断言)

五、简化函数调用

42、Rename Method (函数改名)

43、Add Parameter (添加参数)

44、Remove Parameter (移除参数)

45、Separate Query form Modifier (将查询函数和修改函数分离)

46、Parameterize Method (令函数携带参数)

47、Replace Parameter with Explicit Methods (以明确函数取代参数)

48、Preserve Whole Object (保持对象完整)

49、Replace Parameter with Methods (以函数取代参数)

50、Introduce Parameter Object (引入参数对象)

51、Remove Setting Method (移除设置函数)

52、Hide Method (隐藏函数)

53、Replace Constructor with Factory Method (以工厂函数取代构造函数)

54、Encapsulate Downcast (封装向下转型)

55、Replace Error Code with Exception (以异常取代错误码)

56、Replace Exception with Test (以测试取代异常)

六、处理概括关系

57、Pull Up Field (字段上移)

58、Pull Up Method (方法上移)

59、Pull Up Constructor Body (构造函数本体上移)

60、push down Method (函数下移)

61、push down field (字段下移)

62、Extract Subclass (提炼子类)

63、Extract Superclass (提炼超类)

64、Extract Interface (提炼接口)

65、Collapse Hierarch (折叠继承体系)

66、Form Template Method (塑造模板函数)

67、Replace Inheritance with Delegation (以委托取代继承)

68、Replace Delegation with Inheritance (以继承取代委托)