-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (51 loc) · 1.41 KB
/
app.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
class Subject {
constructor(observers = []) {
this._observers = observers;
}
subscribe(observerCallback) {
this._observers.push(observerCallback);
}
unsubscribe(observerCallback) {
const observerCallbackIndex = this._observers.indexOf(observerCallback);
if (observerCallbackIndex !== -1) {
this._observers.splice(observerCallbackIndex, 1)
}
}
notify(data) {
this._observers.forEach((observerCallback) => observerCallback(data))
}
}
class LampModel extends Subject{
constructor(isTurnOn = false) {
super()
this._isTurnOn = isTurnOn;
}
switch() {
this._isTurnOn = !this._isTurnOn;
this.notify(this._isTurnOn);
}
}
class LampController {
constructor(model = new LampModel()) {
this._model = model;
}
handleSwich() {
this._model.switch()
}
}
class LampView {
constructor(model = new LampModel()) {
this._model = model;
this._bindedRender = this.render.bind(this)
this._model.subscribe(this._bindedRender)
this._controller = new LampController(this._model);
}
destroy() {
this._model.unsubscribe(this._bindedRender)
}
render(isTurnOn) {
console.log('Лампа', isTurnOn ? 'включена': 'выключена' , this._controller)
}
}
const lampView = new LampView();
lampView.render();