Skip to content

Commit 8df4643

Browse files
committed
done email_notifier_example
1 parent a375817 commit 8df4643

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Email Notifier Example
2+
3+
## With Decorator
4+
5+
<img src = "../assets/Decorator_Design_Pattern_UML.jpg" >
6+
7+
```dart
8+
abstract class Notifier {
9+
void notify();
10+
}
11+
12+
class EmailNotifier implements Notifier {
13+
String _email;
14+
EmailNotifier(String email) : _email = email;
15+
@override
16+
void notify() => print("Sending email to: $_email \n");
17+
}
18+
19+
class NotifierDecorator implements Notifier {
20+
Notifier _notifier;
21+
NotifierDecorator(Notifier notifier) : _notifier = notifier;
22+
23+
Notifier get notifier => _notifier;
24+
set notifier(Notifier notifier) => _notifier = notifier;
25+
26+
notify() => _notifier.notify();
27+
}
28+
29+
class SMSNotifierDecorator extends NotifierDecorator {
30+
String _phoneNumber;
31+
32+
SMSNotifierDecorator(Notifier notifier, String phoneNumber)
33+
: _phoneNumber = phoneNumber,
34+
super(notifier);
35+
36+
@override
37+
void notify() {
38+
super.notify();
39+
_sendSMS();
40+
}
41+
42+
void _sendSMS() => print("Sending SMS to $_phoneNumber \n");
43+
}
44+
45+
class WhatsAppNotifierDecorator extends NotifierDecorator {
46+
String _phoneNumber;
47+
48+
WhatsAppNotifierDecorator(Notifier notifier, String phoneNumber)
49+
: _phoneNumber = phoneNumber,
50+
super(notifier);
51+
@override
52+
void notify() {
53+
super.notify();
54+
_sendSMS();
55+
}
56+
57+
void _sendSMS() => print("Sending WhatsApp message to $_phoneNumber \n");
58+
}
59+
60+
void main(List<String> args) {
61+
bool isSmsNotificationEnabled = true;
62+
bool isWhatsAppNotificationEnabled = true;
63+
Notifier notifier = EmailNotifier("[email protected]");
64+
65+
if (isSmsNotificationEnabled) {
66+
notifier = SMSNotifierDecorator(notifier, "01000");
67+
}
68+
if (isWhatsAppNotificationEnabled) {
69+
notifier = WhatsAppNotifierDecorator(notifier, "01000");
70+
}
71+
72+
notifier.notify();
73+
}
74+
```
75+
## Without Decorator
76+
77+
```dart
78+
abstract class Notifier {
79+
void notify();
80+
}
81+
82+
class EmailNotifier implements Notifier {
83+
String _email;
84+
EmailNotifier(String email) : _email = email;
85+
@override
86+
void notify() => print("Sending email to: $_email \n");
87+
}
88+
89+
// the Phone Notifiers using whats app or sms
90+
abstract class PhoneNotifier implements Notifier {
91+
String _phoneNumber;
92+
PhoneNotifier(String phoneNumber) : _phoneNumber = phoneNumber;
93+
}
94+
95+
class SMSNotifier extends PhoneNotifier {
96+
SMSNotifier(String phoneNumber) : super(phoneNumber);
97+
@override
98+
void notify() => print("Sending SMS to $_phoneNumber \n");
99+
}
100+
101+
class WhatsAppNotifier extends PhoneNotifier {
102+
WhatsAppNotifier(String phoneNumber) : super(phoneNumber);
103+
@override
104+
void notify() => print("Sending WhatsApp message to $_phoneNumber \n");
105+
}
106+
107+
void main(List<String> args) {
108+
bool isSmsNotificationEnabled = true;
109+
bool isWhatsAppNotificationEnabled = true;
110+
Notifier emailNotifier = EmailNotifier("[email protected]");
111+
emailNotifier.notify();
112+
if (isSmsNotificationEnabled) {
113+
Notifier sMSNotifier = SMSNotifier("01000");
114+
sMSNotifier.notify();
115+
}
116+
if (isWhatsAppNotificationEnabled) {
117+
Notifier whatsAppNotifier = WhatsAppNotifier("01000");
118+
whatsAppNotifier.notify();
119+
}
120+
}
121+
122+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
abstract class Notifier {
2+
void notify();
3+
}
4+
5+
class EmailNotifier implements Notifier {
6+
String _email;
7+
EmailNotifier(String email) : _email = email;
8+
@override
9+
void notify() => print("Sending email to: $_email \n");
10+
}
11+
12+
class NotifierDecorator implements Notifier {
13+
Notifier _notifier;
14+
NotifierDecorator(Notifier notifier) : _notifier = notifier;
15+
16+
Notifier get notifier => _notifier;
17+
set notifier(Notifier notifier) => _notifier = notifier;
18+
19+
notify() => _notifier.notify();
20+
}
21+
22+
class SMSNotifierDecorator extends NotifierDecorator {
23+
String _phoneNumber;
24+
25+
SMSNotifierDecorator(Notifier notifier, String phoneNumber)
26+
: _phoneNumber = phoneNumber,
27+
super(notifier);
28+
29+
@override
30+
void notify() {
31+
super.notify();
32+
_sendSMS();
33+
}
34+
35+
void _sendSMS() => print("Sending SMS to $_phoneNumber \n");
36+
}
37+
38+
class WhatsAppNotifierDecorator extends NotifierDecorator {
39+
String _phoneNumber;
40+
41+
WhatsAppNotifierDecorator(Notifier notifier, String phoneNumber)
42+
: _phoneNumber = phoneNumber,
43+
super(notifier);
44+
@override
45+
void notify() {
46+
super.notify();
47+
_sendSMS();
48+
}
49+
50+
void _sendSMS() => print("Sending WhatsApp message to $_phoneNumber \n");
51+
}
52+
53+
void main(List<String> args) {
54+
bool isSmsNotificationEnabled = true;
55+
bool isWhatsAppNotificationEnabled = true;
56+
Notifier notifier = EmailNotifier("[email protected]");
57+
58+
if (isSmsNotificationEnabled) {
59+
notifier = SMSNotifierDecorator(notifier, "01000");
60+
}
61+
if (isWhatsAppNotificationEnabled) {
62+
notifier = WhatsAppNotifierDecorator(notifier, "01000");
63+
}
64+
65+
notifier.notify();
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
abstract class Notifier {
2+
void notify();
3+
}
4+
5+
class EmailNotifier implements Notifier {
6+
String _email;
7+
EmailNotifier(String email) : _email = email;
8+
@override
9+
void notify() => print("Sending email to: $_email \n");
10+
}
11+
12+
// the Phone Notifiers using whats app or sms
13+
abstract class PhoneNotifier implements Notifier {
14+
String _phoneNumber;
15+
PhoneNotifier(String phoneNumber) : _phoneNumber = phoneNumber;
16+
}
17+
18+
class SMSNotifier extends PhoneNotifier {
19+
SMSNotifier(String phoneNumber) : super(phoneNumber);
20+
@override
21+
void notify() => print("Sending SMS to $_phoneNumber \n");
22+
}
23+
24+
class WhatsAppNotifier extends PhoneNotifier {
25+
WhatsAppNotifier(String phoneNumber) : super(phoneNumber);
26+
@override
27+
void notify() => print("Sending WhatsApp message to $_phoneNumber \n");
28+
}
29+
30+
void main(List<String> args) {
31+
bool isSmsNotificationEnabled = true;
32+
bool isWhatsAppNotificationEnabled = true;
33+
Notifier emailNotifier = EmailNotifier("[email protected]");
34+
emailNotifier.notify();
35+
if (isSmsNotificationEnabled) {
36+
Notifier sMSNotifier = SMSNotifier("01000");
37+
sMSNotifier.notify();
38+
}
39+
if (isWhatsAppNotificationEnabled) {
40+
Notifier whatsAppNotifier = WhatsAppNotifier("01000");
41+
whatsAppNotifier.notify();
42+
}
43+
}

0 commit comments

Comments
 (0)