重构 笔记

2018-03-16  本文已影响7人  caia

本书针对单进程软件,书中的重构方法不一定适用于分布式软件

第一章 ,重构——第一个案例

  • 代码结构使得添加新特性异常困难,那么需要重构代码,使特性的添加容易进行,然后再添加新特性。
  • 重构的第一步是编写可靠的测试机制,这些测试必须有自我检查能力。
  • 重构时最好小步前进,降低犯错几率。重构的节奏:小修改,测试,小修改,测试......

第二章,重构原则

1.何谓重构

对软件内部结构的一种调整,目的是在不改变软件可观察行为的前提下,提高其可理解性,降低其修改成本。
重构和添加新功能是两种不同的行为,不能同时进行。重构后再添加新功能。

2.为何重构

3.何时重构

4.重构的难题

第三章,代码的坏味道

1.重复代码

Extract method/class
Form Template Method

2.过长函数

Extract method
Replace Temp with Query消除临时变量
Introduce Parameter Object,Preserve whole Object缩减参数
Replace Method with Method Object
Decompose Conditional

3.Large Class 大类

Extract Class/SubClass/Interface

4.Long Parameter Lists过长的参数列

Introduce Parameter Object
Preserve whole Object

5.Divergent Change发散式变化

一个类受多种变化的影响
Extract Class将变化提炼到另一个类中

6.Shotgun Change

一种变化引发多个类的修改
Move Method/Field将需要修改的代码放到一个类中

7.Feature Envy

函数同时依赖几个类的数据
Extract Method分解函数

8.Data Clumps数据泥团

总是同时出现的数据对
Introduce Parameter Object
Preserve whole Object

9.Primitive Obsession基本类型偏执

Replace DataValue with Object
Replace Type Code with Class/Strategy/State

10.Switch Statements

Replace Conditional with Polymorphism
Replace Parameter with Explicit Methods
Introduce Null Object

11.Parallel Inheritance Hierarchies平行继承体系

相似类
让一个类引用另一个类的实例,Move Method/Field

12.Lazy Class冗余类

Collapse Hierarchy, Inline Class

13.Speculative Generality

删除多余的函数、参数、类、接口
Collapse Hierarchy, Inline Class

14.临时字段

Extract Class将变量和相关函数提炼到一个类中

15.Message Chains过度耦合的消息链

Hide Delegate, Extract Method, Move Method

16.Middle Man

Inline Method, Remove Middle Man, Replace Delegation with Inheritance

17.Inappropriate Intimacy

耦合类
Move Methods/Field
Extract Class
Replace Inheritance with Delegation

18.Alternative Classes with Different Interfaces

Rename/Move Method
Extract SuperClass

19.Incomplete Library Class

修改类库函数
Introduce Foreign Method
Introduce Local Extension

20.Data Class

封装、隐藏数据类字段、容器、函数

21.Refused Bequest子类拒绝部分父类接口/实现

Replace Inheritance with Delegation

22.Comments

通过命名、调用关系代替注释
Extract Method
Rename Method
Introduce Assertion

第四章,构筑测试体系

  • 自动化测试,自动化检查结果
  • JUnit测试框架
  • 针对容易出错的地方编写测试用例
  • 频繁运行测试
  • 花合理的时间找出大部分bug

第六章,重新组织函数

1.Extract Method

2.Inline Method

3.Inline Temp内联临时变量

4.Replace Temp with Query以查询取代临时变量

5.Introduce Explaining Variable引入解释性变量

6.分解临时变量

7.Remove Assignments to Parameters

Java 对象的引用本质上是按值传递的。因此可以修改参数对象的内部状态,但对参数对象重新赋值是不没有意义的。

8.Replace Method with Method Object

9.替换算法

第七章,在对象之间搬移特性

1.Move Method

2.Move Field

3.Extract Class

4.Inline Class

5.Hide Delegate 隐藏委托关系

原:
manager = john.getDepartment().getManager();
新:
public Persion getManager(){
  reutrn _department.getManager()
}

6. Remove Middle Man移除中间件

7. Introduce Foreign Method

8. Introduce Local Extension引入本地扩展

第八章,重新组织数据

1.Self Encapsulate Field自封装字段

2.Replace Data Value with Object

3.Replace Value to Reference值对象改为引用对象

4.Replace Reference to Value

5.Replace Array with Object

6.Duplicate Observed Data

7.单向关联到双向关联

8.双向关联到单向关联

9.字面常量取代魔法数

10.封装字段

11.封装集合

12.数据类取代记录

13.以类取代类型码

14.以子类取代类型码

15.以State/Strategy取代类型码

16.以字段取代子类

第九章,简化条件表达式

1.分解条件表达式

2.合并条件表达式

3.合并重复的分支片段

4.移除控制标记

5.Replace Nested Conditional with Guard Clauses

6.多态取代条件表达式

7.引入Null对象

8.引入断言

第十章,简化函数调用

1.Rename Method

2.添加参数

3.移除参数

4.Seperate Query from Modifier

5.Parameterize Method

6.以明确函数取代参数

7.保持参数中的完整对象

8.函数取代参数

9.引入参数对象

10.去除设值函数

11.隐藏函数

12.工厂函数替代构造函数

13.封装向下转型

14.异常取代错误码

15.测试取代异常

第十一章,处理概括关系

1.字段上移

2.函数上移

3.构造函数上移

4.函数下移

5.字段下移

6.提炼子类

7.提炼超类

8.提炼接口

9.折叠继承关系

10.Form Template Method

11.委托取代继承

12.Replace Delegation with Inheritance

第十二章,大型重构

1.Tease Apart Inheritance梳理并分解继承体系

2.Convert Procedural Design to Objects

3.Separate Domain from Presentation

4.建立继承体系

上一篇 下一篇

猜你喜欢

热点阅读