-
Notifications
You must be signed in to change notification settings - Fork 74
/
Mediator.js
81 lines (70 loc) · 2.23 KB
/
Mediator.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
79
80
81
'use strict';
class Mediator {
constructor() {
console.log('Mediator Class created');
}
colleagueChanged(colleague) {
console.log('Mediator.colleagueChanged invoked');
}
}
class ConcreteMediator extends Mediator {
constructor() {
super();
console.log('ConcreteMediator Class created');
this.colleague1 = new ConcreteColleague1(this);
this.colleague2 = new ConcreteColleague2(this);
}
colleagueChanged(colleague) {
console.log('ConcreteMediator.colleagueChanged invoked');
switch (colleague) {
case this.colleague1:
console.log('ConcreteColleague1 has Changed -> change ConcreteColleague2.feature: ');
this.colleague2.setFeature('new feature 2');
break
case this.colleague2:
console.log('ConcreteColleague2 has Changed, but do nothing');
break
default:
console.log('Do nothing');
}
}
}
class Colleague {
constructor() {
console.log('Colleague Class created');
}
changed() {
console.log('Colleague.changed invoked');
this.mediator.colleagueChanged(this);
}
}
class ConcreteColleague1 extends Colleague {
constructor(mediator) {
super();
console.log('ConcreteColleague1 Class created');
this.mediator = mediator;
this.feature = "feature 1";
}
setFeature(feature) {
console.log('ConcreteColleague1.setFeature invoked');
console.log('ConcreteColleague1 Feature has changed from ' + this.feature + ' to ' + feature)
this.feature = feature;
this.changed();
}
}
class ConcreteColleague2 extends Colleague {
constructor(mediator) {
super();
console.log('ConcreteColleague2 Class created');
this.mediator = mediator;
this.feature = "feature 2";
}
setFeature(feature) {
console.log('ConcreteColleague2.setFeature invoked');
console.log('ConcreteColleague2 Feature has changed from ' + this.feature + ' to ' + feature);
this.feature = feature;
this.changed();
}
}
var mediator = new ConcreteMediator();
mediator.colleague1.setFeature("new feature 1");