一、《重构》改变面向对象设计的方式
1、改进软件设计(整理代码)
2、提高代码质量和可读性,使软件系统更易理解和维护
3、帮助尽早的发现错误
4、提高编程速度
改善设计、提高可读性、减少缺陷都是为了稳住阵脚。良好的设计是成功的一半,停下来通过重构改进设计,或许会在当前减缓速度,但它带来的后发优势却是不可低估的。
二. 编写代码22宗罪
- Duplicated Code重复代码
- Long Method 过长函数
- Large Class 过大的类
- Long Parameter List 过长参数列
- Divergent Change 发散式变化(相对聚焦式)
- Shotgun Surgery 霰弹式修改
- Feature Envy 依恋情节
- Data Clumps 数据泥团
- Primitive Obsession 基本类型偏执
- Switch Statement (switch 惊悚现身)
- Parallel Inheritance Hierarchies 平衡继承体系
- Lazy Class(冗赘类)
- Speculative Generality(夸夸其谈未来性)
- Temporary Field(令人迷惑的暂时值域)
- Middle Man(中间转手人)
- Inappropriate Intimacy(狎昵关系)
- Alternative Classes with Different Interfaces(异曲同工的类)
- Incomplete Library Class(不完善的库类)
- Local Extension (引入本地扩展) 。
- Data Class(纯稚的数据类)
- Refused Bequest(被拒绝的遗赠)
三、重新组织函数的九种方法
- Extract Method 提炼函数
- Inline Method 内联函数
- Inline Temp 内联临时变量
- Replace Temp with Query 以查询代替临时变量
- Introduce Explaining Variable 引入解释性变量
- Split Temporary Variable 分解临时变量
- Remove Assigments to Parameters 移除对参数的赋值
- Replace Method with Method object 函数对象取代函数
- Substitute Algorithm 替换算法
四、对象之间移动特性的八种方法
- Move Method 移动函数
- Move Field 搬移字段
- Extract Class提炼类
- Inline Class 将类内联化
- Hide Delegate 隐藏委托关系
- Remove middle Man 移除中间人
- Introduce Foreign Method 引入外加函数
- Introduce Local Extension 引入本地扩展
五、重新组织数据的16种方法
- Self Encapsulate Field 自封装字段
- Replace Data Value with Object 对象取代数据值
- Change value to Reference 将值对象改为引用对象
- Change Reference to Value 将引用对象改为值对象
- Replace Array with Object 以对象取代数组
- Duplicate Observed data 复制被监视数据
- Change Unidirection Association to Bidirectional 将单向关联改为双向关联
- Change Bidirectional Association to Unidirection将双向关联改为单向关联
- Replace Magic Number with Symbolic Constant字面常量取代魔法数
- Encapsulate Field 封装字段
- Encapsulate Coolection 封装集合
- Replace Record with Data Class 以数据类取代记录
- Replace Type Code with Class 以类来取代类型码
- Replace Type Code with Subclasses 以子类来取代类型码
- Replace Type Code with State/Strategy 以状态/策略取代类型码
- Replace Subclass with Fieldls 以字段取代子类
六、简化条件表达式
- Decompose Conditional 分解条件表达式
- Consolidate Conditional Expression 合并条件表达式
- Consolodate Duplicate Conditional Fragments 合并重复的条件片段
- Remove Control Flag 移除控制标记
- Replace Nested Conditional with Guard Clauses 以卫语句取代嵌套条件表达式
- Replace Conditional with Polymorphism 以多态取代条件表达式
- Introduce Null Object 引入Null对象
- Introduce Assertion 引入断言
七、简化函数调用
- Rename Method 函数改名
- Add Parameter 添加参数
- Remove Parameter 移除参数
- Separate Query from Modifier 将查询函数和修改函数分离
- Parameterize Method 令函数携带参数
- Replace Parameter with Explicit Methods 以明确函数取代参数
- Preserve whole object 保持对象完整
- Replace Parameter with Methods 以函数取代参数
- Introduce Parameter Object 引入参数对象
- Remove setting Method 移除设置函数
- Hide Method 隐藏函数
- Replace Constructor with Factory Method 以工厂函数取代构造函数
- Encapsulate Downcast 封装向下转型
- Replace Error Code with Exception 以异常取代错误码
- Replace Exception with Test 以测试取代异常
八、处理概括关系
- Pull Up Field 字段上移
- Pull up Method 函数上移
- Pull up Constructor Body 构造函数本体上移
- Push down Method 函数下移
- Push down Fiedld 字段下移
- Extract Subclass 提炼子类
- Extract Superclass 提炼超类
- Extract Interface 提炼接口
- Collapse Hierarchy 折叠继承体系
- From TemPlate Method 塑造模板函数
- Replace Inheritance with delegation 以委托取代继承
- Replace delegation with Inheritance 以继承代替委托
九、大型重构
- Tease apart Inheritance 梳理并分解继承体系
- Convert Procedural design to Objects 将过程化设计转化为对象设计
- Separate Domain from from Presention 将领域和表述/显示分离
- Extract Hierarchy 提炼继承体系