-
Notifications
You must be signed in to change notification settings - Fork 2
/
TemplateMethod.ts
46 lines (38 loc) · 881 Bytes
/
TemplateMethod.ts
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
class BaseClass {
public templateMethod(): void {
this.actionA();
this.actionB();
}
public actionA(): void {
throw new Error('should not be invoker by BaseClass');
};
public actionB(): void {
throw new Error('should not be invoker by BaseClass');
}
}
class ConcreteAClass extends BaseClass {
actionA(): void {
console.log('A take actionA')
}
actionB(): void {
console.log('A take actionB')
}
}
class ConcreteBClass extends BaseClass {
actionA(): void {
console.log('B take actionA')
}
actionB(): void {
console.log('B take actionB')
}
}
// USAGE:
const a = new ConcreteAClass();
const b = new ConcreteBClass();
a.templateMethod();
b.templateMethod();
// OUTPUT:
// "A take actionA"
// "A take actionB"
// "B take actionA"
// "B take actionB"