-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator.ts
52 lines (44 loc) · 1.13 KB
/
mediator.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
49
50
51
52
interface Mediator {
notify(event: string): void
}
class ComponentHTML {
protected mediator!: Mediator;
someEvent() { };
setMediator(mediator: Mediator) {
this.mediator = mediator;
};
}
class ConcreteMediator implements Mediator {
private elem1: ComponentHTML;
private elem2: ComponentHTML;
constructor(elem1: ComponentHTML, elem2: ComponentHTML) {
this.elem1 = elem1;
this.elem2 = elem2;
this.elem1.setMediator(this);
this.elem2.setMediator(this);
}
notify(event: string) {
if (event === 'A') {
this.elem2.someEvent();
}
if (event === 'B') {
this.elem1.someEvent();
}
}
}
class ConcreteComponent1 extends ComponentHTML {
someEvent() {
console.log('some event 1 comp');
this.mediator.notify('A');
}
}
class ConcreteComponent2 extends ComponentHTML {
someEvent() {
console.log('some event 2 comp');
}
}
const comp1 = new ConcreteComponent1();
const comp2 = new ConcreteComponent2();
const med = new ConcreteMediator(comp1, comp2);
comp1.someEvent();
comp2.someEvent();