-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.7 Observer.ts
222 lines (176 loc) · 5.33 KB
/
3.7 Observer.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
abstract class Observer {
abstract update(subject: Subject): void;
}
abstract class Subject {
protected observers!: Observer[];
constructor() {
this.observers = [];
}
attach(observer: Observer): void {
this.observers.push(observer);
}
detach(observer: Observer): void {
this.observers = this.observers.filter(o => o !== observer);
}
notify(): void {
for (const observer of this.observers) {
observer.update(this);
}
}
}
class ClockTimer extends Subject {
updateInterval: number = 1000;
hours: number;
minutes: number;
seconds: number;
constructor() {
super();
this.hours = (new Date()).getHours();
this.minutes = (new Date()).getMinutes();
this.seconds = (new Date()).getSeconds();
}
start(): void {
setInterval(() => this.tick(), this.updateInterval);
}
tick(): void {
this.seconds++;
if (this.seconds > 59) {
this.seconds = 0;
this.minutes++;
if (this.minutes > 59) {
this.minutes = 0;
this.hours++;
if (this.hours > 23) {
this.hours = 0;
}
}
}
this.notify();
}
getHours(): number {
return this.hours;
}
getMinutes(): number {
return this.minutes;
}
getSeconds(): number {
return this.seconds;
}
}
class Thermometer extends Subject {
updateInterval: number = 4000;
temperature: number;
constructor() {
super();
this.temperature = this.getTemperature();
}
start(): void {
setInterval(() => this.tick(), this.updateInterval);
}
tick(): void {
this.temperature = this.getTemperature();
this.notify();
}
getTemperature(): number {
const now = new Date();
const hourWithFractions = now.getHours() + now.getMinutes() / 60 + now.getSeconds() / 3600;
return this.temperatureFunction(hourWithFractions);
}
temperatureFunction(hourWithFractions: number): number {
const x = hourWithFractions;
const temperature = 10
+ 2 * (-4 * Math.sin(0.3 * x) - Math.sin(-2.5 * Math.E * x) + 2 * Math.sin(-0.3 * Math.PI * x))
- 10 * Math.sin(x / 100);
return this.roundBy(temperature, 2);
}
roundBy(num: number, digits: number): number {
const multiplier = 10 ** digits;
return Math.round(num * multiplier) / multiplier;
}
}
class Clock {
hours: number;
minutes: number;
seconds: number;
constructor() {
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
draw(): void {
console.log(`Drew clock with a time of ${this.padded(this.hours)}:${this.padded(this.minutes)}:${this.padded(this.seconds)}.`);
}
padded(num: number): string {
let s = String(num);
while (s.length < 2) { s = "0" + s; }
return s;
}
}
class DigitalClockWithTemperature extends Clock implements Observer {
timer!: ClockTimer;
thermometer!: Thermometer;
hours: number;
minutes: number;
seconds: number;
temperature: number;
constructor(timer: ClockTimer, thermometer: Thermometer) {
super();
this.timer = timer;
timer.attach(this);
this.thermometer = thermometer;
thermometer.attach(this);
this.hours = timer.getHours();
this.minutes = timer.getMinutes();
this.seconds = timer.getSeconds();
this.temperature = thermometer.getTemperature();
this.draw();
}
update(subject: Subject): void {
if (subject === this.timer) {
const timer = subject as ClockTimer;
this.hours = timer.getHours();
this.minutes = timer.getMinutes();
this.seconds = timer.getSeconds();
} else if (subject === this.thermometer) {
const thermometer = subject as Thermometer;
this.temperature = thermometer.getTemperature();
}
this.draw();
}
draw(): void {
console.log(`Drew analog clock with a time of ${this.padded(this.hours)}:${this.padded(this.minutes)}:${this.padded(this.seconds)} and temperature of ${this.temperature} \u2103.`);
}
}
class AnalogClock extends Clock implements Observer {
timer!: ClockTimer;
hours: number;
minutes: number;
seconds: number;
constructor(timer: ClockTimer) {
super();
this.timer = timer;
timer.attach(this);
this.hours = timer.getHours();
this.minutes = timer.getMinutes();
this.seconds = timer.getSeconds();
this.draw();
}
update(subject: Subject): void {
if (subject === this.timer) {
const timer = subject as ClockTimer;
this.hours = timer.getHours();
this.minutes = timer.getMinutes();
this.seconds = timer.getSeconds();
}
this.draw();
}
draw(): void {
console.log(`Drew analog clock with a time of ${this.padded(this.hours)}:${this.padded(this.minutes)}:${this.padded(this.seconds)}.`);
}
}
const timer = new ClockTimer();
timer.start();
const thermometer = new Thermometer();
thermometer.start();
new DigitalClockWithTemperature(timer, thermometer);
new AnalogClock(timer);