Update 设计模式 - 模板方法.md

小节标题改为中文,重新绘制类图,补充参考资料。
This commit is contained in:
CyC2018
2026-10-28 03:19:59 +08:00
parent ac386a6d96
commit ca233c416c
2 changed files with 11 additions and 10 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

+11 -10
View File
@@ -1,17 +1,14 @@
## 模板方法(Template Method)
### Intent
### 意图
定义算法框架,并将一些步骤的实现延迟到子类。
通过模板方法,子类可以重新定义算法的某些步骤,而不用改变算法的结构。
### Class Diagram
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/ac6a794b-68c0-486c-902f-8d988eee5766.png"/> </div><br>
### Implementation
### 类图
<div align="center"> <img src="pics/dp-template.png" width="760px"/> </div><br>
### 实现
冲咖啡和冲茶都有类似的流程,但是某些步骤会有点不一样,要求复用那些相同步骤的代码。
<div align="center"> <img src="pics/template-recipe.png" width="740px"/> </div><br>
@@ -80,7 +77,7 @@ public class Client {
}
```
```html
```text
boilWater
Coffee.brew
pourInCup
@@ -92,9 +89,13 @@ pourInCup
Tea.addCondiments
```
### JDK
### JDK 中的应用
- java.util.Collections#sort()
- java.io.InputStream#skip()
- java.io.InputStream#read()
- java.util.AbstractList#indexOf()
## 参考资料
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.