-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path15-模板方法模式.js
92 lines (75 loc) · 1.85 KB
/
15-模板方法模式.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
为防止子类没有重写父类中定义的方法,可在父类方法中抛出异常
*/
// 定义一个抽象类
class Beverage {
boilWater () {
console.log('把水煮沸')
}
brew () {
throw new Error('空方法,必须由子类重写');
}
pourInCup () {
throw new Error('空方法,必须由子类重写');
}
addCondiments () {
throw new Error('空方法,必须由子类重写');
}
init () {
this.boilWater();
this.brew();
this.pourInCup();
this.addCondiments();
}
}
// 创建咖啡子类
class Coffee extends Beverage {
brew () {
console.log('用沸水冲泡咖啡');
}
pourInCup () {
console.log('把咖啡倒进杯子');
}
addCondiments () {
console.log('加糖和牛奶');
}
}
const coffee = new Coffee();
coffee.init();
// JavaScript版本
const Beverage1 = function (params) {
const boilWater = params.boilWater || function() {
console.log('把水煮沸')
}
const brew = params.brew || function() {
throw new Error('空方法,必须由子类重写');
}
const pourInCup = params.pourInCup || function() {
throw new Error('空方法,必须由子类重写');
}
const addCondiments = params.addCondiments || function() {
throw new Error('空方法,必须由子类重写');
}
const F = function () {}
F.prototype.init = () => {
boilWater();
brew();
pourInCup();
addCondiments();
}
return F;
}
const Tea = Beverage1({
brew () {
console.log('用沸水浸泡茶叶');
},
pourInCup () {
console.log('把茶倒进杯子');
},
addCondiments () {
console.log('加柠檬');
}
})
const tea = new Tea();
tea.init();