Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f96f553

Browse files
committed
template pattern
1 parent 4ccfd09 commit f96f553

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

Template/template.html

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<script>
2+
var Coffee = function(){};
3+
Coffee.prototype.boilwater = function(){
4+
console.log('把水煮沸');
5+
};
6+
Coffee.prototype.brewCoffeeGriends = function(){
7+
console.log('用沸水冲咖啡');
8+
};
9+
Coffee.prototype.pourInCup = function(){
10+
console.log('把咖啡倒进杯子');
11+
};
12+
Coffee.prototype.addSugarAndMilk = function(){
13+
console.log('加糖和牛奶');
14+
};
15+
Coffee.prototype.init = function(){
16+
this.boilwater();
17+
this.brewCoffeeGriends();
18+
this.pourInCup();
19+
this.addSugarAndMilk();
20+
};
21+
var coffee = new Coffee();
22+
coffee.init();
23+
24+
var Tea = function(){};
25+
Tea.prototype.boilWater = function(){
26+
console.log('把水煮沸');
27+
};
28+
Tea.prototype.steepTeaBag = function(){
29+
console.log('用沸水浸泡茶叶');
30+
};
31+
Tea.prototype.pourInCup = function(){
32+
console.log('把茶水倒进杯子');
33+
};
34+
Tea.prototype.addLemon = function(){
35+
console.log('加柠檬');
36+
};
37+
Tea.prototype.init = function(){
38+
this.boilWater();
39+
this.steepTeaBag();
40+
this.pourInCup();
41+
this.addLemon();
42+
};
43+
var tea = new Tea();
44+
tea.init();
45+
</script>
46+
47+
<script>
48+
var Beverage = function(){};
49+
Beverage.prototype.boilWater = function(){
50+
console.log('把水煮沸');
51+
};
52+
53+
Beverage.prototype.brew = function(){}; // 空方法,应该由子类重写
54+
Beverage.prototype.pourInCup = function(){};
55+
Beverage.prototype.addCondiments = function(){};
56+
Beverage.prototype.init = function(){
57+
this.boilWater();
58+
this.brew();
59+
this.pourInCup();
60+
this.addCondiments();
61+
};
62+
63+
var Coffee1 = function(){};
64+
Coffee1.prototype = new Beverage();
65+
66+
Coffee1.prototype.brew = function(){
67+
console.log('用沸水冲咖啡');
68+
};
69+
Coffee1.prototype.pourInCup = function(){
70+
console.log('把咖啡倒进杯子');
71+
};
72+
Coffee1.prototype.addCondiments = function(){
73+
console.log('加糖和牛奶');
74+
};
75+
var coffee1 = new Coffee1();
76+
coffee1.init();
77+
78+
var Tea1 = function(){};
79+
Tea1.prototype = new Beverage();
80+
Tea1.prototype.brew = function(){
81+
console.log('用沸水浸泡茶叶');
82+
};
83+
Tea1.prototype.pourInCup = function(){
84+
console.log('把茶倒进杯子');
85+
};
86+
Tea1.prototype.addCondiments = function(){
87+
console.log('加柠檬');
88+
};
89+
var tea1 = new Tea1();
90+
tea1.init();
91+
</script>
92+
93+
<script>
94+
var Beverage2 = function(){};
95+
Beverage2.prototype.boilWater = function(){
96+
console.log('把水煮沸');
97+
};
98+
Beverage2.prototype.brew = function(){
99+
throw new Error('子类必须重写brew 方法');
100+
};
101+
Beverage2.prototype.pourInCup = function(){
102+
throw new Error('子类必须重写pourInCup 方法');
103+
};
104+
Beverage2.prototype.addCondiments = function(){
105+
throw new Error('子类必须重写addCondiments 方法');
106+
};
107+
Beverage2.prototype.customerWantsCondiments = function(){
108+
return true;
109+
};
110+
Beverage2.prototype.init = function(){
111+
this.boilWater();
112+
this.brew();
113+
this.pourInCup();
114+
if(this.customerWantsCondiments()){ // 如果挂钩返回true,则需要调料
115+
this.addCondiments();
116+
}
117+
};
118+
var CoffeeWithHook = function(){};
119+
CoffeeWithHook.prototype = new Beverage2();
120+
CoffeeWithHook.prototype.brew = function(){
121+
console.log('用沸水冲咖啡');
122+
};
123+
CoffeeWithHook.prototype.pourInCup = function(){
124+
console.log('把咖啡倒进杯子');
125+
};
126+
CoffeeWithHook.prototype.addCondiments = function(){
127+
console.log('加糖和牛奶');
128+
};
129+
CoffeeWithHook.prototype.customerWantsCondiments = function(){
130+
return Window.confirm('请问需要调料吗?');
131+
};
132+
var coffeewithhook = new CoffeeWithHook();
133+
</script>
134+
135+
<script>
136+
var Beverage3 = function(param){
137+
var boilWater = function(){
138+
console.log('把水煮沸');
139+
};
140+
var brew = param.brew ||function(){
141+
throw new Error('必须传递brew方法');
142+
};
143+
var pourInCup = param.pourInCup || function(){
144+
throw new Error('必须传递pourInCup 方法');
145+
};
146+
var addCondiments = param.addCondiments || function(){
147+
throw new Error('必须传递addCondiments 方法');
148+
};
149+
var F = function(){};
150+
F.prototype.init = function(){
151+
boilWater();
152+
brew();
153+
pourInCup();
154+
addCondiments();
155+
};
156+
return F;
157+
};
158+
var Coffee3 = Beverage3({
159+
brew:function(){
160+
console.log('用沸水冲泡咖啡');
161+
},
162+
pourInCup:function(){
163+
console.log('把咖啡倒进杯子');
164+
},
165+
addCondiments:function(){
166+
console.log('加糖和牛奶');
167+
}
168+
});
169+
170+
var Tea3 = Beverage3({
171+
brew:function(){
172+
console.log('用沸水浸泡茶叶');
173+
},
174+
pourInCup:function(){
175+
console.log('把茶倒进杯子');
176+
},
177+
addCondiments:function(){
178+
console.log('加柠檬');
179+
}
180+
});
181+
182+
var coffeee = new Coffee3();
183+
coffeee.init();
184+
var teaa = new Tea3();
185+
teaa.init();
186+
</script>

0 commit comments

Comments
 (0)