-
Notifications
You must be signed in to change notification settings - Fork 33
/
observer.ats
80 lines (63 loc) · 1.73 KB
/
observer.ats
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
80
import { Logger } from '../logger';
import { int } from '../lang';
export class Subject {
constructor() {
this.observers = [];
this.logger = new Logger();
}
register(observer:Observer) {
this.logger.log(observer.toString() + " is pushed!");
this.observers.push(observer);
}
unregister(observer:Observer) {
var n:int = this.observers.indexOf(observer);
this.logger.log(observer.toString() + " is removed");
this.observers.splice(n, 1);
}
notify() {
this.logger.log("notify all the observers");
for (var i = 0, max = this.observers.length; i < max; i += 1) {
this.observers[i].notify();
}
}
}
export class ConcreteSubject extends Subject {
get SubjectState():int {
return this.subjectState;
}
set SubjectState(subjectState:int) {
this.subjectState = subjectState;
}
}
export class Observer {
constructor() {
this.logger = new Logger();
}
notify() {
throw new Error("Abstract Method!");
}
toString():string {
}
}
export class ConcreteObserver extends Observer {
constructor(subject:ConcreteSubject, name:string) {
super();
this.logger.log("ConcreteObserver " + name + " is created!");
this.subject = subject;
this.name = name;
}
notify() {
this.logger.log("ConcreteObserver's notify method");
this.logger.log(this.name, this.state);
this.state = this.subject.SubjectState;
}
get Subject():ConcreteSubject {
return this.subject;
}
set Subject(subject:ConcreteSubject) {
this.subject = subject;
}
toString():string {
return this.name;
}
}