-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathMediator.js
39 lines (32 loc) · 881 Bytes
/
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
/*
It is a behavioural design pattern that encapsulates how a set of object interact with each other. It provides the central authority over a group of objects by promoting loose coupling by keeping objects from referring each other explicitly.
*/
class TrafficTower {
constructor() {
this._airplanes = [];
}
register(airplane) {
this._airplanes.push(airplane);
airplane.register(this);
}
requestCoordinates(airplane) {
return this._airplanes.filter(plane => airplane !== plane).map(plane => plane.coordinates);
}
}
class Airplane {
constructor(coordinates) {
this.coordinates = coordinates;
this.trafficTower = null;
}
register(trafficTower) {
this.trafficTower = trafficTower;
}
requestCoordinates() {
if (this.trafficTower) return this.trafficTower.requestCoordinates(this);
return null;
}
}
module.exports = {
TrafficTower,
Airplane,
};