-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-simple.js
More file actions
58 lines (47 loc) · 1.1 KB
/
2-simple.js
File metadata and controls
58 lines (47 loc) · 1.1 KB
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
'use strict';
class Colleague {
constructor(mediator) {
this.mediator = mediator;
}
send(message) {
this.mediator.send(message, this);
}
}
class Employee extends Colleague {
constructor(mediator) {
super(mediator);
mediator.employee = this;
}
notify(message) {
if (!this.mediator) throw new Error('Mediator is not set');
console.log('Employee gets message: ' + message);
}
}
class Manager extends Colleague {
constructor(mediator) {
super(mediator);
mediator.manager = this;
}
notify(message) {
if (!this.mediator) throw new Error('Mediator is not set');
console.log('Manager gets message: ' + message);
}
}
class Mediator {
constructor() {
this.employee = null;
this.manager = null;
}
send(message, sender) {
const { employee, manager } = this;
const target = sender === manager ? employee : manager;
target.notify(message);
}
}
// Usage
const mediator = new Mediator();
const employee = new Employee(mediator);
const manager = new Manager(mediator);
console.dir(mediator);
employee.send('Ping');
manager.send('Pong');