-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-command-pattern.py
More file actions
363 lines (258 loc) · 10.1 KB
/
01-command-pattern.py
File metadata and controls
363 lines (258 loc) · 10.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""Question: Create a class Command that uses the Command pattern to
encapsulate a request as an object. Implement commands for TurnOn and TurnOff.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What is the Command pattern and why is it useful?
# - How do you create a base class that defines an interface?
# - What method should all commands implement?
# - How do you create concrete command classes that inherit from the base?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the base Command class
# ===============================================================================
# Explanation:
# Let's start by creating our base Command class. This class defines the interface
# that all concrete commands must implement. It uses the Command pattern to
# encapsulate requests as objects.
class Command:
pass # We'll add methods next
# What we accomplished in this step:
# - Created the basic Command class structure
# Step 2: Add the execute method to the base class
# ===============================================================================
# Explanation:
# The execute method is the core of the Command pattern. All concrete commands
# must implement this method. We'll make it abstract by raising NotImplementedError.
class Command:
def execute(self):
raise NotImplementedError("Subclasses must implement this method")
# What we accomplished in this step:
# - Added abstract execute method that must be overridden
# - Provides clear error message for unimplemented methods
# Step 3: Create the TurnOn command
# ===============================================================================
# Explanation:
# Now let's create our first concrete command. The TurnOn command will inherit
# from Command and implement the execute method with specific behavior.
class Command:
def execute(self):
raise NotImplementedError("Subclasses must implement this method")
class TurnOn(Command):
def execute(self):
return "Turning on"
# What we accomplished in this step:
# - Created TurnOn class that inherits from Command
# - Implemented execute method with specific "turn on" behavior
# Step 4: Create the TurnOff command
# ===============================================================================
# Explanation:
# Let's create our second concrete command. The TurnOff command will also inherit
# from Command but implement different behavior.
class Command:
def execute(self):
raise NotImplementedError("Subclasses must implement this method")
class TurnOn(Command):
def execute(self):
return "Turning on"
class TurnOff(Command):
def execute(self):
return "Turning off"
# What we accomplished in this step:
# - Created TurnOff class that inherits from Command
# - Implemented execute method with specific "turn off" behavior
# Step 5: Test our Command pattern implementation
# ===============================================================================
# Explanation:
# Now let's test our Command pattern by creating instances of our commands
# and executing them. This demonstrates how commands can be treated uniformly.
class Command:
def execute(self):
raise NotImplementedError("Subclasses must implement this method")
class TurnOn(Command):
def execute(self):
return "Turning on"
class TurnOff(Command):
def execute(self):
return "Turning off"
# Test our Command pattern:
print("=== Basic Command Pattern Test ===")
commands = [TurnOn(), TurnOff()]
for command in commands:
result = command.execute()
print(f"{command.__class__.__name__}: {result}")
# What we accomplished in this step:
# - Created instances of both command types
# - Executed commands in a loop, treating them uniformly
# - Demonstrated polymorphism with the Command pattern
# Step 6: Enhanced version with device context
# ===============================================================================
# Explanation:
# Let's create an enhanced version that includes a device to operate on.
# This makes the commands more realistic and demonstrates how commands
# can encapsulate both the action and the target.
class Command:
def execute(self):
raise NotImplementedError("Subclasses must implement this method")
def undo(self):
raise NotImplementedError("Subclasses must implement this method")
class Device:
def __init__(self, name):
self.name = name
self.is_on = False
def turn_on(self):
if not self.is_on:
self.is_on = True
return f"{self.name} is now ON"
return f"{self.name} is already ON"
def turn_off(self):
if self.is_on:
self.is_on = False
return f"{self.name} is now OFF"
return f"{self.name} is already OFF"
def get_status(self):
status = "ON" if self.is_on else "OFF"
return f"{self.name} is {status}"
class TurnOnCommand(Command):
def __init__(self, device):
self.device = device
def execute(self):
return self.device.turn_on()
def undo(self):
return self.device.turn_off()
class TurnOffCommand(Command):
def __init__(self, device):
self.device = device
def execute(self):
return self.device.turn_off()
def undo(self):
return self.device.turn_on()
# Test enhanced version:
print("\n=== Enhanced Command Pattern with Device ===")
# Create devices
tv = Device("Smart TV")
lights = Device("Living Room Lights")
print("Initial status:")
print(tv.get_status())
print(lights.get_status())
# Create commands
tv_on = TurnOnCommand(tv)
tv_off = TurnOffCommand(tv)
lights_on = TurnOnCommand(lights)
lights_off = TurnOffCommand(lights)
# Execute commands
print("\nExecuting commands:")
print(tv_on.execute())
print(lights_on.execute())
print(tv.get_status())
print(lights.get_status())
print("\nExecuting more commands:")
print(tv_off.execute())
print(lights_off.execute())
print("\nTesting undo functionality:")
print(tv_off.undo()) # Should turn TV back on
print(lights_off.undo()) # Should turn lights back on
# What we accomplished in this step:
# - Created a Device class to represent controllable devices
# - Enhanced commands to work with specific devices
# - Added undo functionality to commands
# - Demonstrated more realistic command usage
# Step 7: Command invoker with history
# ===============================================================================
# Explanation:
# Let's create a command invoker that can execute commands and maintain
# a history for undo operations. This completes the Command pattern implementation.
class RemoteControl:
def __init__(self):
self.history = []
def execute_command(self, command):
result = command.execute()
self.history.append(command)
print(f"Executed: {result}")
return result
def undo_last_command(self):
if self.history:
last_command = self.history.pop()
result = last_command.undo()
print(f"Undid: {result}")
return result
else:
print("No commands to undo")
def get_history_size(self):
return len(self.history)
# Test complete Command pattern with invoker:
print("\n=== Complete Command Pattern with Remote Control ===")
# Create devices and remote
stereo = Device("Stereo System")
fan = Device("Ceiling Fan")
remote = RemoteControl()
# Create commands
stereo_on = TurnOnCommand(stereo)
stereo_off = TurnOffCommand(stereo)
fan_on = TurnOnCommand(fan)
fan_off = TurnOffCommand(fan)
# Use remote to execute commands
print("Using remote control:")
remote.execute_command(stereo_on)
remote.execute_command(fan_on)
remote.execute_command(stereo_off)
print(f"\nHistory size: {remote.get_history_size()}")
print("\nUndoing commands:")
remote.undo_last_command() # Undo stereo off (turn it back on)
remote.undo_last_command() # Undo fan on (turn it back off)
remote.undo_last_command() # Undo stereo on (turn it back off)
remote.undo_last_command() # No more commands to undo
# What we accomplished in this step:
# - Created RemoteControl class as a command invoker
# - Added command history tracking
# - Implemented undo functionality with history
# - Demonstrated complete Command pattern usage
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Understanding the Command pattern and its benefits
# - Creating abstract base classes with NotImplementedError
# - Implementing concrete command classes
# - Encapsulating requests as objects
# - Adding undo functionality to commands
# - Creating command invokers with history
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding macro commands that execute multiple commands!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================