-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3_dip.py
More file actions
50 lines (33 loc) · 1.25 KB
/
problem3_dip.py
File metadata and controls
50 lines (33 loc) · 1.25 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
"""
Problem 3: Dependency Inversion Principle (DIP)
"""
from abc import ABC, abstractmethod
# ---------- BAD DESIGN ----------
class EmailNotification:
def send_notification(self, message):
print("Email:", message)
class BadNotificationManager:
def notify(self, message):
email = EmailNotification()
email.send_notification(message)
# ---------- GOOD DESIGN ----------
class NotificationService(ABC):
@abstractmethod
def send_notification(self, message): pass
class EmailNotification(NotificationService):
def send_notification(self, message):
print("Email:", message)
class SMSNotification(NotificationService):
def send_notification(self, message):
print("SMS:", message)
class PushNotification(NotificationService):
def send_notification(self, message):
print("Push:", message)
class NotificationManager:
def notify_user(self, user, message, service: NotificationService):
service.send_notification(f"{user}: {message}")
if __name__ == "__main__":
manager = NotificationManager()
manager.notify_user("Fara", "Hello!", EmailNotification())
manager.notify_user("Fara", "Hello!", SMSNotification())
manager.notify_user("Fara", "Hello!", PushNotification())