代码味道及重构手段
来源:互联网 发布:mac序列号查询激活日期 编辑:程序博客网 时间:2024/06/10 03:30
Martin Fowler针对22种代码味道,提出了相应的重构手段,。具体而言,如下表所示:
代码味道
常用重构手段
接口不同的相似类(Alternative Classes with Different Interfaces)【异曲同工的类】
● 重命名方法(Rename Method)
● 搬移方法(Move Method)
注释(Comments)【过多的注释】
● 抽取方法(Extract Method)
● 引入断言(Introduce Assertion)
数据类(Data Class)【纯稚的数据类】
● 搬移方法(Move Method)
● 封装字段(Encapsulate Field)
● 封装集合(Encapsulate Collection)
数据泥团(Data Clump)
● 抽取类(Extract Class)
● 引入参数对象(Introduce Parameter Object)
● 保持对象完整(Preserve Whole Object)
发散式改变(Divergent Change)
● 抽取类(Extract Class)
重复性代码(Duplicated Code)
● 抽取方法(Extract Method)
● 抽取类(Extract Class)
● 上移方法(Pull Up Method)
● 构建模板方法(Form Template Method)
依恋情结(Feature Envy)
● 搬移方法(Move Method)
● 搬移字段(Move Field)
● 抽取方法(Extract Method)
不当的紧密性(Inappropriate Intimacy)【狎昵关系】
● 搬移方法(Move Method)
● 搬移字段(Move Field)
● 将双向关联改为单向(Change Bidirectional Association to Unidirectional)
● 将继承替换成委托(Replace Inheritance with Delegation)
● 隐藏委托(Hide Delegate)
不完备的库类(Incomplete Library Class)
● 引入外来方法(Introduce Foreign Method)、
● 引入本地扩展(Introduce Local Extension)
过大的类(Large Class)
● 抽取类(Extract Class)
● 抽取子类(Extract Subclass)
● 抽取接口(Extract Interface)
● 将数据值替换为对象(Replace Data Value with Object)
懒惰类(Lazy Class)【冗赘类】
● 内联类(Inline Class)
● 折叠继承体系(Collapse Hierarchy)
过长的方法(Long Method)
● 抽取方法(Extract Method)
● 将临时变量替换为查询(Replace Temp with Query)
● 将方法替换为方法对象(Replace Method with Method Object)
● 分解条件式(Decompose Conditional)
过长的参数表(Long Parameter List)
● 将参数替换为方法(Replace Parameter with Method)
● 引入参数对象(Introduce Parameter Object)
● 保持对象完整(Preserve Whole Object)
消息链(Message Chains)【过渡耦合的消息链】
● 隐藏委托(Hide Delegate)
中间人(Middle Man)
● 移除中间人(Remove Middle Man)
● 内联方法(Inline Method)
● 将委托替换为继承(Replace Delegation with Inheritance)
并行继承体系(Parallel Inheritance Hierarchies)
● 搬移方法(Move Method)
● 搬移字段(Move Field)
基本类型困扰(Primitive Obsession)【基本类型偏执】
● 将数据值替换为对象(Replace Data Value with Object)
● 抽取类(Extract Class)
● 引入参数对象(Introduce Parameter Object)
● 将数组替换为对象(Replace Array with Object)
● 将类型码替换为类(Replace Type Code with Class)
● 将类型码替换为子类(Replace Type Code with Subclasses)
● 将类型码替换为状态/策略(Replace Type Code with State/Strategy)
拒收的遗赠(Refused Bequest)
● 将继承替换为委托(Replace Inheritance with Delegation)
霰弹式修改(Shotgun Surgery)
● 搬移方法(Move Method)
● 搬移字段(Move Field)
● 内联类(Inline Class)
过分一般性(Speculative Generality)【夸夸其谈的未来性】
● 折叠继承体系(Collapse Hierarchy)
● 内联类(Inline Class)
● 移除参数(Remove Parameter)
● 重命名方法(Rename Method)
Switch语句(Switch Statements)【Switch惊悚现身】
● 将条件式替换为多态(Replace Conditional with Polymorphism)
● 将类型码替换为子类(Replace Type Code with Subclasses)
● 将类型码替换为状态/策略(Replace Type Code with State/Strategy)
● 将参数替换为显式方法(Replace Parameter with Explicit Methods)
● 引入Null对象(Introduce Null Object)
临时字段(Temporary Field)【令人迷惑的暂时值域】
● 抽取类(Extract Class)
● 引入Null对象(Introduce Null Object)
参考资料:
【作者:刘伟 http://blog.csdn.net/lovelion】
- 代码味道及重构手段
- 代码重构 & 代码中的坏味道
- 重构之代码坏味道
- 代码的坏味道,重构,模式
- 代码坏味道,重构与模式
- 代码坏味道与重构
- 代码坏味道——重构
- 代码坏味道与重构
- 重构——代码坏味道
- 代码重构-寻找坏味道
- 代码坏味道与重构
- 代码坏味道与重构
- 《重构》代码的坏味道
- 重构---代码的坏味道
- 重构-代码的坏味道
- 代码坏味道与重构
- 重构----代码的坏味道
- 重构 坏代码的味道
- perl 模式匹配总结
- 接口转换--适配器模式
- 关于图形书籍
- 关于二分图的最大匹配(最小点覆盖)问题的资料整理
- 模板方法模式
- 代码味道及重构手段
- Qt同步线程
- hdu(1498和2119)气球,最小点覆盖
- HDU 1281 棋盘游戏 二分图最大匹配 + 枚举
- Java——Servlet的配置和测试
- 在Vmware9上安装MAC OS X Mountain Lion 过程-最新版
- 最小匹配hdu 3991 Harry Potter and the Present
- HDU1068 (最大独立集)Girls and Boys
- hdu-1528 Card Game Cheater(最大匹配)