-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator-pattern.ts
79 lines (65 loc) · 1.59 KB
/
mediator-pattern.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
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
abstract class Colleague {
protected mediator: Mediator;
abstract id: number;
join(mediator: Mediator) {
this.mediator = mediator;
mediator.addColleague(this);
}
sendData(data: string) {
if (!this.mediator) {
throw new Error('mediator is null');
}
this.mediator.mediate(data);
}
abstract handle(data: string);
}
class ConcreteColleague extends Colleague {
id: number;
constructor() {
super();
this.id = Math.random();
}
handle(data: string) {
console.log(this.id, data);
}
}
abstract class Mediator {
colleagues: Colleague[] = [];
addColleague(colleague: Colleague) {
if (!colleague) {
throw new Error('colleague is null');
}
this.colleagues.push(colleague);
}
abstract mediate(data: string);
}
class ConcreteMediator extends Mediator {
mediate(data: string) {
this.colleagues.forEach((colleague) => {
// 중재 기능 추가
colleague.handle(data);
});
}
}
const App = () => {
const mediator = new ConcreteMediator();
const colleague1 = new ConcreteColleague();
const colleague2 = new ConcreteColleague();
const colleague3 = new ConcreteColleague();
colleague1.join(mediator);
colleague2.join(mediator);
colleague3.join(mediator);
colleague1.sendData('111');
// 0.46935089666482166 111
// 0.5558008755307144 111
// 0.08946524585346682 111
colleague2.sendData('222');
// 0.46935089666482166 222
// 0.5558008755307144 222
// 0.08946524585346682 222
colleague3.sendData('333');
// 0.46935089666482166 333
// 0.5558008755307144 333
// 0.08946524585346682 333
};
App();