File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
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!" ) ;
You can’t perform that action at this time.
0 commit comments