代码改变世界

重构 - 一般化

2016-03-09 22:23  yoogo  阅读(217)  评论(0编辑  收藏  举报

Pull Up Field

Pull Up Method

Pull Up Constructor Body

 

Push Down Method

Push Down Field

Extract Subclass

划分子类要看按什么标准划分子类,比如对于Employee, 可以按职责划分财务,行政,工程师;也可以按等级划分成初级,中级,高级等;这样就出现两套分类体系。如果表现一个中级工程师只能就要考虑组合方式,采用策略模式。

Extract Superclass

Extract Interface

Collapse Hierarchy

Form Template Method

Replace Inheritance with Delegation

问题:子类只需要父类的一部分而不是全部的方法,此时使用继承是不合适的

解:去掉继承关系,把这部分方法委托出去

Replace Delegation with Inheritance

问题:B的方法被A全部委托

解:让 A 继承B