-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiskovSubstitution.py
59 lines (40 loc) · 1.53 KB
/
LiskovSubstitution.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
"""
Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable
with objects of its subclasses without breaking the application.
In other words, what we want is to have the objects of our subclasses behaving the same way
as the objects of our superclass.
"""
from abc import ABC, abstractmethod
class Notification(ABC):
@abstractmethod
def notify(self, message):
raise NotImplementedError
class Email(Notification):
def __init__(self, email):
self.email = email
def notify(self, message):
print(f'Send "{message}" to {self.email}')
class SMS(Notification):
def __init__(self, phone):
self.phone = phone
def notify(self, message):
print(f'Send "{message}" to {self.phone}')
class Contact:
def __init__(self, name, email, phone):
self.name = name
self.email = email
self.phone = phone
class NotificationManager:
def __init__(self, notification):
self.notification = notification
def send(self, message):
self.notification.notify(message)
if __name__ == '__main__':
contact = Contact('John Doe', 'john@test.com', '(408)-888-9999')
sms_notification = SMS(contact.phone)
email_notification = Email(contact.email)
notification_manager = NotificationManager(sms_notification)
notification_manager.send('Hello John')
notification_manager.notification = email_notification
# notification_manager = NotificationManager(email_notification)
notification_manager.send('Hi John')