-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-alerts.ts
More file actions
124 lines (100 loc) · 3.38 KB
/
email-alerts.ts
File metadata and controls
124 lines (100 loc) · 3.38 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
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
// Subject (Publisher)
interface EmailNotifer {
attach(app: LocalEmailApp): void;
detach(app: LocalEmailApp): void;
notify(): void;
}
// Subject
class PaytonEmailNotifer implements EmailNotifer {
public numEmails: number = 0;
private localEmailApps: LocalEmailApp[] = [];
public attach(app: LocalEmailApp): void {
const isExist = this.localEmailApps.includes(app);
if (isExist) {
return console.log('EmailNotifer: Observer has been attached already.');
}
console.log('EmailNotifer: Attached an observer.');
this.localEmailApps.push(app);
}
public detach(app: LocalEmailApp): void {
const appIndex = this.localEmailApps.indexOf(app);
if (appIndex === -1) {
return console.log('EmailNotifer: Nonexistent observer.');
}
this.localEmailApps.splice(appIndex, 1);
console.log('EmailNotifer: Detached an observer.');
}
public notify(): void {
console.log('EmailNotifer: Notifying local apps...');
for (const observer of this.localEmailApps) {
observer.update(this);
}
}
public receive(): void {
const rand = Math.floor(Math.random() * (10 + 2));
this.numEmails += rand
console.log(`EmailNotifer: received ${rand} new emails.`)
this.notify();
}
public read(num: number): void {
this.numEmails -= num;
}
}
// The Observer interface
interface LocalEmailApp {
update(emialNotifer: EmailNotifer): void;
}
class PhoneEmailApp implements LocalEmailApp{
public update(emialNotifer: EmailNotifer): void {
if (emialNotifer instanceof PaytonEmailNotifer) {
console.log('Email App on Phone: You have ' + emialNotifer.numEmails + ' unread messages.');
if(emialNotifer.numEmails > 20){
console.log('Client on Phone: That is a lot, lets read 10 of them.')
emialNotifer.read(10)
}
}
}
}
class ComputerEmailApp implements LocalEmailApp{
public update(emialNotifer: EmailNotifer): void {
if (emialNotifer instanceof PaytonEmailNotifer) {
console.log('Email App on Computer: You have ' + emialNotifer.numEmails + ' unread messages.');
}
}
}
// Client code
const email = new PaytonEmailNotifer();
const phone = new PhoneEmailApp();
email.attach(phone);
const computer = new ComputerEmailApp();
email.attach(computer);
console.log()
email.receive();
console.log()
email.receive();
console.log()
email.receive();
console.log()
email.receive();
console.log()
/* OUTPUT
EmailNotifer: Attached an observer.
EmailNotifer: Attached an observer.
EmailNotifer: received 4 new emails.
EmailNotifer: Notifying local apps...
Email App on Phone: You have 4 unread messages.
Email App on Computer: You have 4 unread messages.
EmailNotifer: received 10 new emails.
EmailNotifer: Notifying local apps...
Email App on Phone: You have 14 unread messages.
Email App on Computer: You have 14 unread messages.
EmailNotifer: received 6 new emails.
EmailNotifer: Notifying local apps...
Email App on Phone: You have 20 unread messages.
Email App on Computer: You have 20 unread messages.
EmailNotifer: received 7 new emails.
EmailNotifer: Notifying local apps...
Email App on Phone: You have 27 unread messages.
Client on Phone: That is a lot, lets read 10 of them.
Email App on Computer: You have 17 unread messages.
*/