`
huobengluantiao8
  • 浏览: 1032486 次
文章分类
社区版块
存档分类
最新评论

《重构-改善既有代码的设计》笔记2

 
阅读更多

一、重构的方向

代码的坏味道Bad smells一章,真是一顿营养丰富的大餐。Duplicated Code是代码腐化的万恶之源,Long Method、Large Class、Long Parameter List这些几乎就是旧社会臭婆娘的裹脚布,Divergent Change、Shotgun Surgery、Feature Envy、Inappropriate Intimacy这些简直就是指责不清勾肩搭背。等等。。。

重构,它为我们清除这些坏味道指明了方向,并且《重构》通过强调“重构是一种有纪律的、经过训练的、有条不紊的程序整理方法”,保证了重构过程的安全性和高效性。

在重构的手法中有相当一大部分是双向的、互逆的,也就是说在某种时候是你找我,而在另一些时候是我找你,比如Pull Up Field和Push Down Field,Pull Up Method和Push Down Method等,而另一些则是强调单向的、勇往直前的,比如Encapsulate Field、Remove Control Flag、Remove Parameter、Remove Assignments to Parameters、Replace Conditional with Polymorphism等。
在双向的、护逆的重构手法中,强调的是一种平衡,是职责清晰,角色明确。
而在强调单向的、勇往直前的重构手法中,突出的是使代码更容易理解、更容易扩展,更加具有面向对象性。

为了对比双向可逆的重构手法,以便突出其侧重点和权衡内容,下面进行列举
重新组织你的函数章节中
Extract Method——Inline Method
Inline Temp——Introduce Explaining Variable

在对象之间搬移特性章节中
Extract Class——Inline Class
Hide Delegate——Remove Middle Man
Introduce Foreign Method——Introduce Local Extension

重新组织数据章节中
Change Value to Reference——Change Reference to Value
Change Unidirectional Association to Bidirectional——Change Bidirectional Association to Unidirectional

简化条件表达式章节中

简化函数调用章节中
Add Parameter——Remove Parameter
Parameterize Method——Replace Parameter with Explicit Methods

处理概括关系章节中
Pull Up Field——Push Down Field
Pull Up Method——Push Down Method
Extract Subclass(Extract Superclass)——Collapse Hierarchy
Replace Inheritance with Delegation——Replace Delegation with Inheritance

在看这本书的过程中感觉这种互逆的挺多,列举之后发现没有之前感觉多。

二、按照采用手法分类

在《重构》中,Martin Fowler按照重构所针对的代码内容进行了分类,包括对函数的重构、对对象特性的重构、对数据组织的重构、对条件表达式的重构以及对类层次的重构,并按照这种分类组织了章节。

除了按照重构所针对的代码内容进行分类外,按照采用手法进行分类,我觉得也可以从另一个角度帮助我们更进一步理解重构的进行方式。

通过整理,按照采用手法分类如下:

1、提炼
就是对某个过程的一部分,或某个事物的一部分进行抽象并概念化,以减小所表达的目的(要做什么)和实现(做什么,如何做)之间的语义差距。

运用提炼的重构手法包括
Extract Method、
Split Temporary Variable、
Extract Class、
Extract Subclass、
Extract Superclass、
Extract Interface等

2、内联
就是用其直接实现替换原来的间接调用,通过内联可以去掉意义不大的间接性。

运用内联的重构手法包括
Inline Method、
Inline Temp、
Inline Class

3、移动
在类之间(包括父类和子类之间)移动属性和方法,以使类之间职责更明确更清晰。

运用移动的重构手法包括
Move Method、
Move Field、
Pull Up Field、
Pull Up Method、
Pull Up Constructor Body、
Push Down Method、
Push Down Field

4、替换
将一种实现方式用另一种实现方式替代,以便于更直接更集中和更灵活

运用替换的重构手法包括
Replace Temp with Query、
Replace Method with Method Object、
Replace Date Value with Object、
Replace Array with Object、
Replace Magic Number with Symbolic Constant、
Replace Record with Data Class、
Replace Type Code with Class、
Replace Type Code with Subclasses、
Replace Type Code with State/Strategy、
Replace Subclass with Fields、
Replace Nested Conditional with Guard Clauses、
Replace Conditional with Polymorphism、
Replace Paramether with Explicit Methods、
Replace Parameter with Method、
Replace Constructor with Factory Method、
Replace Error Code with Exception、
Replace Exception with Test、
Replace Inheritance with Delegation、
Replace Delegation with Inheritance

5、改变
改变和替换语义上有些类似,其间的差别还不是很清楚

运用改变的重构手法包括
Change Value to Reference,
Change Reference to Value,
Change Unidirectional Association to Biderectional
Change Biderectional Association to Unidirectional

6、其他
包括增加Add、移除Remove、隐藏Hide、引入Introduce、封装Encapsulate、合并Consolidate等等

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics