-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand.py
44 lines (29 loc) · 940 Bytes
/
command.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
# ----------------
# Command Pattern.
# ----------------
# Encapsulate a request as an object, thereby allowing for the parameterization of clients with different requests,
# and the queuing or logging of requests. It also allows for the support of undoable operations.
from abc import ABC, abstractmethod
class Command(ABC):
def __init__(self, receiver):
self.receiver = receiver
def process(self):
pass
class CommandImplementation(Command):
def __init__(self, receiver):
self.receiver = receiver
def process(self):
self.receiver.perform_action()
class Receiver:
def perform_action(self):
print('Action performed in receiver.')
class Invoker:
def command(self, cmd):
self.cmd = cmd
def execute(self):
self.cmd.process()
receiver = Receiver()
cmd = CommandImplementation(receiver)
invoker = Invoker()
invoker.command(cmd)
invoker.execute()