File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 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 ( )
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments