-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconceptual.js
More file actions
77 lines (77 loc) · 3.08 KB
/
conceptual.js
File metadata and controls
77 lines (77 loc) · 3.08 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Subject owns a state and notifies Observers (subscribers) when said state changes
var ConcreteSubject = /** @class */ (function () {
function ConcreteSubject() {
// List of subscribers, can be stores in any way with more info
this.observers = [];
}
// Implemenmtations of subscriber (observer) management methods
ConcreteSubject.prototype.attach = function (observer) {
var isExist = this.observers.includes(observer);
if (isExist) {
return console.log('Subject: Observer has been attached already.');
}
console.log('Subject: Attached an observer.');
this.observers.push(observer);
};
ConcreteSubject.prototype.detach = function (observer) {
var observerIndex = this.observers.indexOf(observer);
if (observerIndex === -1) {
return console.log('Subject: Nonexistent observer.');
}
this.observers.splice(observerIndex, 1);
console.log('Subject: Detached an observer.');
};
// Trigger updates to subscribers.
ConcreteSubject.prototype.notify = function () {
console.log('Subject: Notifying observers...');
for (var _i = 0, _a = this.observers; _i < _a.length; _i++) {
var observer = _a[_i];
observer.update(this);
}
};
/**
* Usually, the subscription logic is only a fraction of what a Subject can
* really do. Subjects commonly hold some important business logic, that
* triggers a notification method whenever something important is about to
* happen (or after it).
*/
// Bussiness logic, often this is what triggers the notifications, state may be updated here
ConcreteSubject.prototype.someBusinessLogic = function () {
console.log('\nSubject: I\'m doing something important.');
this.state = Math.floor(Math.random() * (10 + 1));
console.log("Subject: My state has just changed to: ".concat(this.state));
this.notify();
};
return ConcreteSubject;
}());
// Concrete Observers react to the updates from the Subject they are attached to.
var ConcreteObserverA = /** @class */ (function () {
function ConcreteObserverA() {
}
ConcreteObserverA.prototype.update = function (subject) {
if (subject instanceof ConcreteSubject && subject.state < 3) {
console.log('ConcreteObserverA: Reacted to the event.');
}
};
return ConcreteObserverA;
}());
var ConcreteObserverB = /** @class */ (function () {
function ConcreteObserverB() {
}
ConcreteObserverB.prototype.update = function (subject) {
if (subject instanceof ConcreteSubject && (subject.state === 0 || subject.state >= 2)) {
console.log('ConcreteObserverB: Reacted to the event.');
}
};
return ConcreteObserverB;
}());
// Client code
var subject = new ConcreteSubject();
var observer1 = new ConcreteObserverA();
subject.attach(observer1);
var observer2 = new ConcreteObserverB();
subject.attach(observer2);
subject.someBusinessLogic();
subject.someBusinessLogic();
subject.detach(observer2);
subject.someBusinessLogic();