《重构与模式》代码坏味与重构的方法
|
代码坏味 |
重构 |
|
重复代码(Duplicated Code) |
形成Template Method 用Factory Method引入多态创建 链构造函数 用Composite解决一/多之分 提取Composite 通过Adapter统一接口 引入Null Object |
|
方法过长(Long Method) |
组合方法 将聚集操作搬移到Collecting Parameter 用Command替换条件调度程序 将聚集操作搬移到Visitor 用Strategy替换条件逻辑 |
|
条件逻辑太复杂(Conditional Complexity) |
用Strategy替换条件逻辑 将装饰功能搬移到Decorator 用State替换状态改变条件语句 引入Null Object |
|
基本类型迷恋(Primitive Obsession) |
用类替换类型代码 用State替换状态改变条件语句 用Strategy替换条件逻辑 用Composite替换隐含树 用Interpreter替换隐式语言 将装饰功能搬移到Decorator 用Builder封装Composite |
|
不恰当的暴露(Indecent Exposure) |
用Factory封装类 |
|
解决方案蔓延(Solution Sprawl) |
将创建知识搬移到Factory |
|
异曲同工的类(Alternative Classes With Different Interface) |
通过Adapter统一接口 |
|
冗赘类(Lazy Class) |
内联Singleton |
|
类过大(Large Class) |
用Command替换条件调度程序 用State替换状态改变条件语句 用Interpreter替换隐式语言 |
|
分支语句(Switch Statement) |
用Command替换条件调度程序 将聚集操作搬移到Visitor |
|
组合爆炸(Combinatorial Explosion) |
用Interpreter替换隐式语言 |
|
怪异解决方案(Oddball Solution) |
通过Adapter统一接口 |
浙公网安备 33010602011771号