-
Notifications
You must be signed in to change notification settings - Fork 75
/
Decorator.js
78 lines (66 loc) · 1.89 KB
/
Decorator.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
'use strict';
class Component {
constructor() {
console.log('Component Class created');
}
operation() {
console.log('Component.operation invoked');
}
}
class ConcreteComponent extends Component {
constructor() {
super();
console.log('ConcreteComponent Class created');
}
operation() {
console.log('ConcreteComponent.operation invoked');
}
}
class Decorator extends Component {
constructor(component) {
super();
this.component = component;
console.log('Decorator Class created');
}
operation() {
console.log('Decorator.operation invoked');
this.component.operation()
}
}
class ConcreteDecoratorA extends Decorator {
constructor(component, sign) {
super(component);
this.addedState = sign;
console.log('ConcreteDecoratorA Class created');
}
operation() {
super.operation();
console.log('ConcreteDecoratorA.operation invoked');
console.log(this.addedState)
}
}
class ConcreteDecoratorB extends Decorator {
constructor(component, sign) {
super(component);
this.addedState = sign;
console.log('ConcreteDecoratorB Class created');
}
operation() {
super.operation();
console.log('ConcreteDecoratorB.operation invoked');
console.log(this.addedState + this.addedState + this.addedState + this.addedState + this.addedState);
}
addedBehavior() {
this.operation();
console.log('ConcreteDecoratorB.operation invoked');
}
}
var component = new ConcreteComponent();
var decoratorA = new ConcreteDecoratorA(component, 'decoratorA');
var decoratorB = new ConcreteDecoratorB(component, 'decoratorB');
console.log('component: ');
component.operation();
console.log('decoratorA: ');
decoratorA.operation();
console.log('decoratorB: ');
decoratorB.addedBehavior();