forked from torokmark/design_patterns_in_typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.ts
48 lines (38 loc) · 1.19 KB
/
decorator.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
47
48
namespace DecoratorPattern {
export interface Component {
operation(): void;
}
export class ConcreteComponent implements Component {
private s: String;
constructor(s: String) {
this.s = s;
}
public operation(): void {
console.log("`operation` of ConcreteComponent", this.s, " is being called!");
}
}
export class Decorator implements Component {
private component: Component;
private id: Number;
constructor(id: Number, component: Component) {
this.id = id;
this.component = component;
}
public get Id(): Number {
return this.id;
}
public operation(): void {
console.log("`operation` of Decorator", this.id, " is being called!");
this.component.operation();
}
}
export class ConcreteDecorator extends Decorator {
constructor(id: Number, component: Component) {
super(id, component);
}
public operation(): void {
super.operation();
console.log("`operation` of ConcreteDecorator", this.Id, " is being called!");
}
}
}