-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.py
89 lines (56 loc) · 2.86 KB
/
notifications.py
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
from enum import Enum
class NotificationType(Enum):
EMAIL_NOTIFICATION = "EMAIL_NOTIFICATION"
DATABASE_NOTIFICATION = "DATABASE_NOTIFICATION"
REALTIME_NOTIFICATION = "REALTIME_NOTIFICATION"
SMS_NOTIFICATION = "SMS_NOTIFICATION"
class Notification:
def __init__(self, notification_type: NotificationType):
self.adapter = NotificationFactory.get_notification(notification_type)
def notify(self, sender, receiver, message):
self.adapter.send_notification(sender, receiver, message)
# These classes help in implementing adapter pattern
class BaseAdapter:
def send_notification(self, sender, receiver, message):
pass
class DatabaseNotificationAdapter(BaseAdapter):
def send_notification(self, sender, receiver, message):
print(f"Database Notification for email: {receiver.email}, username: {receiver.name} from {sender.email} with "
f"the message: '{message}'")
class EmailNotificationAdapter(BaseAdapter):
def send_notification(self, sender, receiver, message):
print(f"Email Notification for email: {receiver.email}, username: {receiver.name} from {sender.email} with "
f"the message: '{message}'")
class SmsNotificationAdapter(BaseAdapter):
def send_notification(self, sender, receiver, message):
print(f"SMS Notification for email: {receiver.email}, username: {receiver.name} from {sender.email} with the "
f"message: '{message}'")
class RealtimeNotificationAdapter(BaseAdapter):
def send_notification(self, sender, receiver, message):
print(
f"Realtime =Notification for email: {receiver.email}, username: {receiver.name} from {sender.email} with "
f"the message: '{message}'")
# This class implements factory pattern
class NotificationFactory:
@classmethod
def get_notification(cls, notification_type: NotificationType):
if notification_type == NotificationType.SMS_NOTIFICATION.name:
return SmsNotificationAdapter()
elif notification_type == NotificationType.EMAIL_NOTIFICATION.name:
return EmailNotificationAdapter()
elif notification_type == NotificationType.DATABASE_NOTIFICATION.name:
return DatabaseNotificationAdapter()
elif notification_type == NotificationType.REALTIME_NOTIFICATION.name:
return RealtimeNotificationAdapter()
else:
raise Exception("Adapter Type Undefined")
# Mediator Pattern implementation
class NotificationMediator:
def notify(self, sender, receivers, message):
for receiver in receivers:
receiver.get_notified(message, sender)
class NotificationAPIBridge:
def __init__(self):
self.notification_mediator = NotificationMediator()
def send_notification(self, sender, receivers, message):
self.notification_mediator.notify(sender, receivers, message)