Skip to content

Commit 42d2067

Browse files
committed
Template Pattern
1 parent 52283c3 commit 42d2067

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Example/Template-Method-Pattern.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
// 模版方法模式(Template Method Pattern)
1+
// 模版方法模式(Template Method Pattern)
2+
3+
// 定义烧水类
4+
class BoilWater {
5+
constructor () {
6+
7+
}
8+
9+
// 烧水
10+
boilWater = () => {
11+
console.log('I am boil the water!')
12+
}
13+
// 沏茶
14+
makeTea = () => {
15+
console.log('大碗茶,上嘞')
16+
}
17+
init = () => {
18+
this.boilWater()
19+
this.makeTea()
20+
}
21+
}
22+
23+
class Tea extends BoilWater {
24+
constructor () {
25+
super()
26+
}
27+
28+
// 重写沏茶
29+
makeTea = () => {
30+
console.log('头杯茶不喝,喝第三杯')
31+
}
32+
}
33+
34+
let tea = new Tea()
35+
tea.init()

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@
8989

9090
> [模版方法模式(Template Method Pattern)](/Example/Template-Method-Pattern.js)
9191
92+
定义:一个抽象类公开定义了执行它的方法的方式/模板。它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行。
93+
目的:一些方法通用,却在每一个子类都重新写了这一方法。
94+
场景:在造房子的时候,地基、走线、水管都一样,只有在建筑的后期才有加壁橱加栅栏等差异。
95+
9296
> [观察者模式(Observer Pattern)](/Example/Observer-Pattern.js)
9397
9498
> [状态模式(State Pattern)](/Example/State-Pattern.js)

0 commit comments

Comments
 (0)