-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcommand.py
More file actions
67 lines (45 loc) · 1.47 KB
/
command.py
File metadata and controls
67 lines (45 loc) · 1.47 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
"""
Command
- a behavioral design pattern that turns a request into a stand-alone object that
contains all information about the request. This transformation lets you pass requests
as a method arguments, delay or queue a request’s execution, and support undoable operations.
"""
import abc
class Command(abc.ABC):
@abc.abstractmethod
def execute(self):
pass
class SimpleCommand(Command):
def __init__(self, payload):
self._payload = payload
def execute(self):
print(f'SimpleCommand: I can do simple things like printing ({self._payload})')
class ComplexCommand(Command):
def __init__(self, receiver, a, b):
self._receiver = receiver
self._a = a
self._b = b
def execute(self):
print('ComplexCommand: Complex stuff should be done a receiver object', end='')
self._receiver.do_something(self._a)
self._receiver.do_something_else(self._b)
class Receiver:
def do_something(self, a):
print(f'\nReceiver: working on ({a}.)', end="")
def do_something_else(self, b):
print(f'\nReceiver: working on ({b}.)', end="")
class Invoker:
_on_start = None
_on_finish = None
def set_on_start(self, command):
self._on_start = command
def set_on_finish(self, command):
self._on_finish = command
def operation(self):
self._on_start.execute()
self._on_finish.execute()
invoker = Invoker()
invoker.set_on_start(SimpleCommand('Say Hi!'))
receiver = Receiver()
invoker.set_on_finish(ComplexCommand(receiver, 'Send email', 'Save report'))
invoker.operation()