forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslider_command.py
More file actions
180 lines (138 loc) · 5.47 KB
/
slider_command.py
File metadata and controls
180 lines (138 loc) · 5.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""The Command Design Pattern in Python
The command pattern is a behavioural design pattern, in which an abstraction
exists between an object that invokes a command, and the object that performs it.
This is part 2 of the Command Design Pattern tutorial,
where I create a slider, instead of the switch from part 1.
The slider also accepts a variable percentage, rather than an ON/OFF
The history also records the variable settingg
And I also add UNDO/REDO to the Invoker so that you can go backawards and forwards through time
"""
from abc import ABCMeta, abstractstaticmethod
import time
class ICommand(metaclass=ABCMeta):
"""The command interface, which all commands will implement"""
@abstractstaticmethod
def execute(*args):
"""The required execute method which all command objects will use"""
class IUndoRedo(metaclass=ABCMeta):
"""The Undo Redo interface"""
@abstractstaticmethod
def history():
"""the history of the states"""
@abstractstaticmethod
def undo():
"""for undoing the hsitory of the states"""
@abstractstaticmethod
def redo():
"""for redoing the hsitory of the states"""
class Slider(IUndoRedo):
"""The Invoker Class"""
def __init__(self):
self._commands = {}
self._history = [(0.0, "OFF", ())] # A default setting of OFF
self._history_position = 0 # The position that is used for UNDO/REDO
@property
def history(self):
"""Return all records in the History list"""
return self._history
def register(self, command_name, command):
"""All commands are registered in the Invoker Class"""
self._commands[command_name] = command
def execute(self, command_name, *args):
"""Execute a pre defined command and log in history"""
if command_name in self._commands.keys():
self._history_position += 1
self._commands[command_name].execute(args)
if len(self._history) == self._history_position:
# This is a new event in hisory
self._history.append((time.time(), command_name, args))
else:
# This occurs if there was one of more UNDOs and then a new
# execute command happened. In case of UNDO, the history_position
# changes, and executing new commands purges any history after
# the current position"""
self._history = self._history[:self._history_position+1]
self._history[self._history_position] = {
time.time(): [command_name, args]
}
else:
print(f"Command [{command_name}] not recognised")
def undo(self):
"""Undo a command if there is a command that can be undone.
Update the history position so that further UNDOs or REDOs
point to the correct index"""
if self._history_position > 0:
self._history_position -= 1
self._commands[
self._history[self._history_position][1]
].execute(self._history[self._history_position][2])
else:
print("nothing to undo")
def redo(self):
"""Perform a REDO if the history_position is less than the end of the history list"""
if self._history_position + 1 < len(self._history):
self._history_position += 1
self._commands[
self._history[self._history_position][1]
].execute(self._history[self._history_position][2])
else:
print("nothing to REDO")
class Heater:
"""The Receiver"""
def set_to_max(self):
print("Heater is ON and set to MAX (100%)")
def set_to_percent(self, *args):
print(f"Heater is ON and set to {args[0][0]}%")
def turn_off(self):
print("Heater is OFF")
class SliderMaxCommand(ICommand):
"""A Command object, which implements the ICommand interface"""
def __init__(self, heater):
self._heater = heater
def execute(self, *args):
self._heater.set_to_max()
class SliderPercentCommand(ICommand):
"""A Command object, which implements the ICommand interface"""
def __init__(self, heater):
self._heater = heater
def execute(self, *args):
self._heater.set_to_percent(args[0])
class SliderOffCommand(ICommand):
"""A Command object, which implements the ICommand interface"""
def __init__(self, heater):
self._heater = heater
def execute(self, *args):
self._heater.turn_off()
if __name__ == "__main__":
# The Client is the main python app
# The HEATER is the Receiver
HEATER = Heater()
# Create Commands
SLIDER_MAX = SliderMaxCommand(HEATER)
SLIDER_PERCENT = SliderPercentCommand(HEATER)
SLIDER_OFF = SliderOffCommand(HEATER)
# Register the commands with the invoker (Switch)
SLIDER = Slider()
SLIDER.register("MAX", SLIDER_MAX)
SLIDER.register("PERCENT", SLIDER_PERCENT)
SLIDER.register("OFF", SLIDER_OFF)
# Execute the commands that are registered on the Invoker
SLIDER.execute("PERCENT", 10)
SLIDER.execute("PERCENT", 20)
SLIDER.execute("PERCENT", 30)
SLIDER.execute("PERCENT", 40)
SLIDER.execute("PERCENT", 50)
print(SLIDER.history)
SLIDER.undo()
SLIDER.undo()
SLIDER.undo()
SLIDER.redo()
SLIDER.undo()
SLIDER.undo()
SLIDER.execute("PERCENT", 90)
SLIDER.execute("MAX")
SLIDER.execute("OFF")
print(SLIDER.history)
SLIDER.undo()
SLIDER.redo()
print(SLIDER.history)