重构 一书要点

很实用的要点,每次重构都要重新看看这些要点,真正用到平时的日常工作中。

 

 

 

代码坏味道:

  1.duplicate code 重复代码

  2.long method 长方法

  3.large class 过大的类

  4.long parameter list 过长参数列

  5.divergent change 发散式变化

  6.shotgun surgery 散弹式变化

  7 feture envy 依恋情结

  8.data clumps 数据泥团

  9.primitive obsession 基本类型偏执

  10.switch statument switch惊悚现身

  11.parallel inheritance hierarchies 平行继承体系

  12.lazy class 冗债类

  13.speculative generality 夸夸其谈未来性

  14.temparary field 令人迷惑的暂时字段

  15.message chain 过度耦合的消息链

  16.middle man 中间人

  17.inappropriate intimacy 

  18.alernative classes with different intefaces异曲同工的类

  19.incomplete library class 不完美的类库

  20 data class 纯稚的数据类

  21.refused Bequest 被拒绝的遗赠

  22.comments 过多的注释

 

重构手法

 1.extract method 提取函数

 2.inline method 内联函数

 3.inline temp 内联临时变量

 4.replace temp with query 以查询取代临时变量

 5.introduce explaining variable 引入解释性临时变量

 6.splite tempary variable 分解临时变量

 7.remove assignment to parameter 移除对参数的赋值

 8.replace method with method object 以函数对象取代函数

 9.substitute algorithm 替换算法

 

面向对象的重构

1.move method 搬移函数

2.move field 搬移字段

3.extract class 提炼类

4.inline class 内联类

5.hide delegate 隐藏委托关系

6.remove middle man 移除中间人

7.introduce foreign method 引入外加函数

8.introduce local expression 引入本地扩展

 

数据

1.self encapsulate field 自封装字段

2.replace data value with object 以对象取代数据值

3.change value to referrence 将值对象改为引用对象

4.change referrence to value 将引用对象改为值对象

5.replace array with object 以对象代替数组

6.duplicate observerd data 复制被监视对象

7.change unidirectional association to bidirectional 将单向关系改为双向关系  反向指针

8.change bidirectional association to unidirectional 将双向关系改为单向关系 

9.replace magic number with symbolic constant以字面常量取代魔法数

10.encapsulate field 封装字段

11.encapsulate collection 封装集合

12.replace record with data class以数据类取代记录

13.replace type code with class以类取代类型码

14.replace type code with subclass以子类代替类型码

15.replace type code with status/strategy

16.replace subclass with field 以字段取代子类

 

 

简化条件表达式

1.decompose condiction 分解条件表达式

2.consolidate condictional expression 合并条件表达式

3.consolidate duplicate condictional fragments 合并重复的条件片段

4.remove control flag 移除控制标记

5.replace nested condictional with guard clauses 以卫语句取代嵌套条件表达式

6.replace condictional with polymorphism 以多态取代条件表达式

7.introduce null object 引入null对象

8.introduce assertion 引入断言

 

简化函数调用

1.rename method 函数改名

2.add parameter 增加参数

3.remove parameter 删除参数

4.seperate query from modifier

5.parameterize method令函数携带参数

6.replace parameter with explicit methods 以明确函数代替参数

7.preserv whole object 保持对象完整

8.replace parameter with method以方法代替参数

9introduce parameter object 引入参数对象

10.remove setting method 删除设值方法

11.hide method隐藏函数

12.replace constructor with factory method 以工厂函数取代构造函数

13.encapsulate downcast封装向下转型

14.replace error code with exception 以异常代替错误码

15.replace exception with test以测试取代异常

 

处理概括关系(继承关系)

1.pull up field 字段上移

2.pull up method 函数上移

3.pull up constructor body 构造函数本体上移

4.pull down method 函数下移

5.pull down field 字段下移

6.extract subclass 提炼子类

7.extract superclass 提炼超类

8.extract interface 提炼接口

9.collapse hierarchy折叠继承体系

10.form template method 塑造模板函数

11.replace hierarchy with delegate 以委托取代继承

12.replace delegate with inheritance 以继承取代委托

 

大型重构

1.tease apart inheritance梳理并分解继承体系

2.convert procedure design to objects 将过程化设计改为对象化设计

3.seperate domain from presentation 将领域与显示分离

4.extract hierarchy 提炼继承体系

posted @ 2012-06-01 10:53  zhwj184  阅读(189)  评论(0编辑  收藏  举报