Skip to content

Commit fc9b815

Browse files
committed
Add decorator pattern example for solution
1 parent 285f1ad commit fc9b815

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/Decorator/Book/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export interface Notifier {
2+
send(message: string): void;
3+
}
4+
5+
class EmailNotifier implements Notifier {
6+
constructor(private emails: string[]) {}
7+
8+
send(message: string): void {
9+
console.log(`Sending email to ${this.emails}: ${message}`);
10+
}
11+
}
12+
13+
class SMSDecorator implements Notifier {
14+
constructor(private notifier: Notifier) {}
15+
16+
send(message: string): void {
17+
console.log(`Sending SMS: ${message}`);
18+
this.notifier.send(message);
19+
}
20+
}
21+
22+
class FacebookDecorator implements Notifier {
23+
constructor(private notifier: Notifier) {}
24+
25+
send(message: string): void {
26+
console.log(`Sending Facebook message: ${message}`);
27+
this.notifier.send(message);
28+
}
29+
}
30+
31+
class SlackDecorator implements Notifier {
32+
constructor(private notifier: Notifier) {}
33+
34+
send(message: string): void {
35+
console.log(`Sending Slack message: ${message}`);
36+
this.notifier.send(message);
37+
}
38+
}
39+
40+
const emailNotifier = new EmailNotifier([
41+
42+
43+
]);
44+
45+
const combinedNotifier = new SlackDecorator(
46+
new FacebookDecorator(new SMSDecorator(emailNotifier))
47+
);
48+
49+
combinedNotifier.send("Important message: House is on fire!");

0 commit comments

Comments
 (0)