-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcommand.py
More file actions
68 lines (52 loc) · 1.45 KB
/
command.py
File metadata and controls
68 lines (52 loc) · 1.45 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'''
Command Design Pattern - This is behavioral design pattern.
'''
from abc import ABC, abstractmethod
class BaseCommannd(ABC):
'''
Base command class
'''
@abstractmethod
def execute(self):
raise NotImplementedError("Please implement in subclass")
class EMailCommand(BaseCommannd):
'''
Email Command class
'''
def __init__(self, receiver, data):
self.receiver = receiver
self.data = data
def execute(self):
self.receiver.send_email(self.data)
class SMSCommand(object):
'''
Command class
'''
def __init__(self, receiver, data):
self.receiver = receiver
self.data = data
def execute(self):
self.receiver.send_sms(self.data)
class NotificationService(object):
'''
Receiver class
'''
def send_email(self, data):
print("Sending email", data)
def send_sms(self, data):
print("Sending short message", data)
class NotificationInvoker(object):
'''
Invoker class
'''
def __init__(self):
self.notification_history = []
def invoke(self, command):
self.notification_history.append(command)
command.execute()
if __name__ == "__main__":
invoker = NotificationInvoker()
receiver = NotificationService()
invoker.invoke(EMailCommand(receiver, {"subject": "Test Email"}))
invoker.invoke(SMSCommand(receiver, {"subject": "Test SMS"}))
print(invoker.notification_history)