auto commit
This commit is contained in:
26
notes/重构.md
26
notes/重构.md
@ -115,25 +115,25 @@
|
||||
|
||||
影片出租店应用程序,包括三个类:Movie、Rental 和 Customer,Rental 包含租赁的 Movie 以及天数。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//a758c8b2-0ac7-438f-90c2-3923ffad6328.png"/> </div>
|
||||
|
||||
最开始的实现是把所有的计费代码都放在 Customer 类中,在变化发生时,需要对这部分代码进行更改。本案例中可能发生的变化有:一种类别的计费方式发生改变;添加新的电影类别。考虑到计费代码可能存在于多处,一旦发生改变时,就需要对所有计费代码进行修改。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//9e5e3cc6-3107-4051-b584-8ff077638fe6.png"/> </div>
|
||||
|
||||
以下是继承 Movie 的多态方案。但是由于一部 Movie 的类别会动态改变,因此这种方案不可行。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//2a502516-5d34-4eef-8f39-916298a60035.png"/> </div>
|
||||
|
||||
引入 Price 来反应类别信息,通过组合的方式在 Movie 中加入 Price 对象,这样每种类别的计费方式都封装在不同的 Price 子类中,并且 Movie 对象也可以动态改变类别。这种方式可以很好地适应上述提到的变化。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//c02a83b8-a6b9-4d00-a509-6f0516beaf5e.png"/> </div>
|
||||
|
||||
重构后的时序图和类图:
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//95f4559c-3d2a-4176-b365-4fbc46c76cf1.png"/> </div>
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//293b9326-02fc-4ad8-8c79-b4a7b5ba60d3.png"/> </div>
|
||||
|
||||
# 第二章 重构原则
|
||||
|
||||
@ -487,7 +487,7 @@ Hide Delegate 有很大好处,但是它的代价是:每当客户要使用受
|
||||
|
||||
将该数据赋值到一个领域对象中,建立一个 Oberver 模式,用以同步领域对象和 GUI 对象内的重复数据。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//e024bd7e-fb4e-4239-9451-9a6227f50b00.jpg"/> </div>
|
||||
|
||||
## 7. Change Unidirectional Association to Bidirectional(将单向关联改为双向关联)
|
||||
|
||||
@ -544,13 +544,13 @@ public 字段应当改为 private,并提供相应的访问函数。
|
||||
|
||||
类中有一个数值类型码,但它并不影响类的行为,就用一个新类替换该数值类型码。如果类型码出现在 switch 语句中,需要使用 Replace Conditional with Polymorphism 去掉 switch,首先必须运用 Replace Type Code with Subcalss 或 Replace Type Code with State/Strategy 去掉类型码。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//27c2e0b3-8f95-453d-bedc-6398a8566ce9.jpg"/> </div>
|
||||
|
||||
## 14. Replace Type Code with Subcalsses(以子类取代类型码)
|
||||
|
||||
有一个不可变的类型码,它会影响类的行为,以子类取代这个类型码。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//c41d3977-e0e7-4ee4-93e1-d84f1ae3e20e.jpg"/> </div>
|
||||
|
||||
## 15. Replace Type Code with State/Strategy (以 State/Strategy 取代类型码)
|
||||
|
||||
@ -558,13 +558,13 @@ public 字段应当改为 private,并提供相应的访问函数。
|
||||
|
||||
和 Replace Type Code with Subcalsses 的区别是 Replace Type Code with State/Strategy 的类型码是动态可变的,前者通过继承的方式来实现,后者通过组合的方式来实现。因为类型码可变,如果通过继承的方式,一旦一个对象的类型码改变,那么就要改变用新的对象来取代旧对象,而客户端难以改变新的对象。但是通过组合的方式,改变引用的状态类是很容易的。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//81fd1d6f-a3b2-4160-9a0a-1f7cb50ba440.jpg"/> </div>
|
||||
|
||||
## 16. Replace Subclass with Fields(以字段取代子类)
|
||||
|
||||
各个子类的唯一差别只在“返回常量数据”的函数上。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//f2e0cee9-ecdc-4a96-853f-d9f6a1ad6ad1.jpg"/> </div>
|
||||
|
||||
# 第九章 简化条件表达式
|
||||
|
||||
@ -684,7 +684,7 @@ double getSpeed() {
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//1c8432c8-2552-457f-b117-1da36c697221.jpg"/> </div>
|
||||
|
||||
## 7. Introduce Null Object(引入Null对象)
|
||||
|
||||
@ -824,7 +824,7 @@ double finalPrice = discountedPrice (basePrice);
|
||||
|
||||
以一个对象取代这些参数。
|
||||
|
||||

|
||||
<div align="center"> <img src="https://github.com/CyC2018/InterviewNotes/blob/master/pics//08738dd0-ae8e-404a-ba78-a6b1b7d225b3.jpg"/> </div>
|
||||
|
||||
## 10. Remove Setting Method(移除设值函数)
|
||||
|
||||
|
Reference in New Issue
Block a user