-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.py
138 lines (108 loc) · 3.41 KB
/
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from abc import ABC, abstractmethod
class Command(ABC):
"""COMMAND INTERFACE
The switch interface, that all commands will implement
"""
_backup = None
def __init__(self, light: "Light") -> None: # The _light is the Receiver
self._light = light
def backup(self):
self._backup = self._light.state
def undo(self):
self._light.state = self._backup
@abstractmethod
def execute(self) -> None:
pass
class SwitchOnCommand(Command):
"""SWITCH ON COMMAND
A Command object, that implements the Command interface and runs the command on the designated Receiver
"""
def execute(self) -> None:
self.backup()
print("Turning light on...")
self._light.turnOn()
class SwitchOffCommand(Command):
"""SWITCH OFF COMMAND
A Command object, that implements the Command interface and runs the command on the designated Receiver
"""
def execute(self) -> None:
self.backup()
print("Turning light off...")
self._light.turnOff()
class Light:
"""RECEIVER
Implements the methods called by Commands through the execute()
"""
_state = 0
def turnOn(self) -> None:
self.state = 1
def turnOff(self) -> None:
self.state = 0
@property
def state(self) -> int:
return self._state
@state.setter
def state(self, state: int) -> None:
self._state = state
def showState(self) -> None:
if self.state:
print("\033[92;1m" + "Light is ON" + "\033[0m")
else:
print("\033[31;1m" + "Light is OFF" + "\033[0m")
class Switch:
"""INVOKER
The Invoker is associated with one or several commands. It sends a request to the command.
"""
def __init__(self) -> None:
self._commands = {}
self._history = []
def showHistory(self) -> None:
"""
Print the command history
"""
for index, commandName in enumerate(self._history):
print(f"{index}:\t{commandName}")
def registerCommand(self, commandName: str, command: Command) -> None:
"""
Add a command to the invoker
"""
self._commands[commandName] = command
def executeCommand(self, commandName: str):
"""
Execute a registered command
"""
if commandName in self._commands.keys():
self._commands[commandName].execute()
self._history.append(self._commands[commandName])
else:
print(f"Command [{commandName}] not recognised")
def undo(self):
if len(self._history):
print("Undoing last command...")
lastCommand = self._history[-1]
lastCommand.undo()
self._history.pop(-1)
else:
print("No commands to undo!")
if __name__ == "__main__":
# Create the Receiver (a Light)
light = Light()
# Create Commands
switchOn = SwitchOnCommand(light)
switchOff = SwitchOffCommand(light)
# Create the Invoker (a Switch) and register Commands
switch = Switch()
switch.registerCommand("on", switchOn)
switch.registerCommand("off", switchOff)
# Execute the commands
light.showState()
switch.executeCommand("on")
light.showState()
switch.executeCommand("off")
light.showState()
switch.undo()
light.showState()
switch.undo()
light.showState()
switch.undo()
light.showState()