-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-state-pattern.py
More file actions
475 lines (335 loc) · 13.7 KB
/
02-state-pattern.py
File metadata and controls
475 lines (335 loc) · 13.7 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
"""Question: Implement a class State that uses the State pattern to
change its behavior when its internal state changes.
"""
# 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 State pattern and when is it useful?
# - How do you create an abstract base state class?
# - How do concrete states implement different behaviors?
# - How does the context delegate behavior to the current state?
#
# 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 abstract State class
# ===============================================================================
# Explanation:
# Let's start by creating the abstract State class. This defines the interface
# that all concrete states must implement.
class State:
def handle(self):
raise NotImplementedError("Subclasses must implement this method")
# What we accomplished in this step:
# - Created abstract State class with handle method
# - Used NotImplementedError to enforce implementation in subclasses
# Step 2: Create concrete state classes
# ===============================================================================
# Explanation:
# Now let's create concrete state classes that implement different behaviors.
# Each state will handle requests differently.
class State:
def handle(self):
raise NotImplementedError("Subclasses must implement this method")
class ConcreteStateA(State):
def handle(self):
return "State A handling request"
class ConcreteStateB(State):
def handle(self):
return "State B handling request"
# What we accomplished in this step:
# - Created ConcreteStateA and ConcreteStateB classes
# - Each implements the handle method with different behavior
# Step 3: Create the Context class
# ===============================================================================
# Explanation:
# The Context class holds a reference to the current state and delegates
# behavior to that state. It can also change states.
class State:
def handle(self):
raise NotImplementedError("Subclasses must implement this method")
class ConcreteStateA(State):
def handle(self):
return "State A handling request"
class ConcreteStateB(State):
def handle(self):
return "State B handling request"
class Context:
def __init__(self, state):
self._state = state
def set_state(self, state):
self._state = state
def request(self):
return self._state.handle()
# What we accomplished in this step:
# - Created Context class that holds current state
# - Added methods to change state and delegate requests
# Step 4: Test our basic State pattern
# ===============================================================================
# Explanation:
# Let's test our State pattern by creating a context with different states
# and observing how behavior changes.
class State:
def handle(self):
raise NotImplementedError("Subclasses must implement this method")
class ConcreteStateA(State):
def handle(self):
return "State A handling request"
class ConcreteStateB(State):
def handle(self):
return "State B handling request"
class Context:
def __init__(self, state):
self._state = state
def set_state(self, state):
self._state = state
def request(self):
return self._state.handle()
# Test our basic State pattern:
print("=== Testing Basic State Pattern ===")
context = Context(ConcreteStateA())
print(f"Initial state: {context.request()}")
context.set_state(ConcreteStateB())
print(f"After changing to State B: {context.request()}")
context.set_state(ConcreteStateA())
print(f"After changing back to State A: {context.request()}")
# What we accomplished in this step:
# - Created context with initial state
# - Demonstrated state changes and different behaviors
# - Verified that pattern works correctly
# Step 5: Enhanced State pattern with state transitions
# ===============================================================================
# Explanation:
# Let's create a more realistic example with a traffic light that automatically
# transitions between states and has different behaviors for each state.
class TrafficLightState:
def handle_request(self, context):
raise NotImplementedError("Subclasses must implement this method")
def next_state(self, context):
raise NotImplementedError("Subclasses must implement this method")
def get_color(self):
raise NotImplementedError("Subclasses must implement this method")
class RedState(TrafficLightState):
def handle_request(self, context):
return "RED: Stop! Do not proceed."
def next_state(self, context):
print("RED -> GREEN: Light changing to green")
context.set_state(GreenState())
def get_color(self):
return "RED"
class YellowState(TrafficLightState):
def handle_request(self, context):
return "YELLOW: Caution! Prepare to stop."
def next_state(self, context):
print("YELLOW -> RED: Light changing to red")
context.set_state(RedState())
def get_color(self):
return "YELLOW"
class GreenState(TrafficLightState):
def handle_request(self, context):
return "GREEN: Go! Proceed with caution."
def next_state(self, context):
print("GREEN -> YELLOW: Light changing to yellow")
context.set_state(YellowState())
def get_color(self):
return "GREEN"
class TrafficLight:
def __init__(self):
self._state = RedState() # Start with red
self.cycle_count = 0
def set_state(self, state):
self._state = state
def get_instruction(self):
return self._state.handle_request(self)
def get_current_color(self):
return self._state.get_color()
def change_light(self):
self._state.next_state(self)
self.cycle_count += 1
def get_status(self):
return {
'color': self.get_current_color(),
'instruction': self.get_instruction(),
'cycle_count': self.cycle_count
}
# Test enhanced State pattern:
print("\n=== Enhanced State Pattern with Traffic Light ===")
traffic_light = TrafficLight()
print("Traffic light simulation:")
for i in range(8): # Go through multiple cycles
status = traffic_light.get_status()
print(f"Cycle {i+1}: {status['color']} - {status['instruction']}")
traffic_light.change_light()
print(f"\nTotal cycles completed: {traffic_light.cycle_count}")
# What we accomplished in this step:
# - Created realistic traffic light with automatic state transitions
# - Each state knows how to transition to the next state
# - Demonstrated state-specific behavior and transitions
# - Added cycle counting and status reporting
# Step 6: State pattern with conditional transitions
# ===============================================================================
# Explanation:
# Let's create a more complex example with a document workflow that has
# conditional state transitions based on user roles and document content.
class DocumentState:
def edit(self, context, user_role):
raise NotImplementedError()
def submit(self, context, user_role):
raise NotImplementedError()
def approve(self, context, user_role):
raise NotImplementedError()
def reject(self, context, user_role):
raise NotImplementedError()
def get_status(self):
raise NotImplementedError()
class DraftState(DocumentState):
def edit(self, context, user_role):
if user_role in ["author", "editor"]:
return "Document edited successfully"
return "Access denied: Cannot edit document"
def submit(self, context, user_role):
if user_role in ["author", "editor"]:
context.set_state(UnderReviewState())
return "Document submitted for review"
return "Access denied: Cannot submit document"
def approve(self, context, user_role):
return "Cannot approve: Document is still in draft"
def reject(self, context, user_role):
return "Cannot reject: Document is still in draft"
def get_status(self):
return "DRAFT"
class UnderReviewState(DocumentState):
def edit(self, context, user_role):
return "Cannot edit: Document is under review"
def submit(self, context, user_role):
return "Document is already submitted"
def approve(self, context, user_role):
if user_role == "reviewer":
context.set_state(ApprovedState())
return "Document approved"
return "Access denied: Only reviewers can approve"
def reject(self, context, user_role):
if user_role == "reviewer":
context.set_state(DraftState())
return "Document rejected, returned to draft"
return "Access denied: Only reviewers can reject"
def get_status(self):
return "UNDER_REVIEW"
class ApprovedState(DocumentState):
def edit(self, context, user_role):
return "Cannot edit: Document is approved"
def submit(self, context, user_role):
return "Document is already approved"
def approve(self, context, user_role):
return "Document is already approved"
def reject(self, context, user_role):
if user_role == "admin":
context.set_state(DraftState())
return "Document rejected by admin, returned to draft"
return "Access denied: Only admins can reject approved documents"
def get_status(self):
return "APPROVED"
class Document:
def __init__(self, title):
self.title = title
self._state = DraftState()
self.history = []
def set_state(self, state):
old_status = self._state.get_status()
self._state = state
new_status = self._state.get_status()
self.history.append(f"{old_status} -> {new_status}")
def edit(self, user_role):
result = self._state.edit(self, user_role)
self.history.append(f"Edit attempt by {user_role}: {result}")
return result
def submit(self, user_role):
result = self._state.submit(self, user_role)
self.history.append(f"Submit attempt by {user_role}: {result}")
return result
def approve(self, user_role):
result = self._state.approve(self, user_role)
self.history.append(f"Approve attempt by {user_role}: {result}")
return result
def reject(self, user_role):
result = self._state.reject(self, user_role)
self.history.append(f"Reject attempt by {user_role}: {result}")
return result
def get_status(self):
return self._state.get_status()
def get_history(self):
return self.history
# Test document workflow:
print("\n=== Document Workflow with Conditional State Transitions ===")
doc = Document("Project Proposal")
print(f"Document: {doc.title}")
print(f"Initial status: {doc.get_status()}")
# Author edits and submits
print(f"\n1. {doc.edit('author')}")
print(f"2. {doc.submit('author')}")
print(f" Status: {doc.get_status()}")
# Editor tries to edit (should fail)
print(f"\n3. {doc.edit('editor')}")
# Reviewer approves
print(f"4. {doc.approve('reviewer')}")
print(f" Status: {doc.get_status()}")
# Author tries to edit approved doc (should fail)
print(f"\n5. {doc.edit('author')}")
# Admin rejects approved doc
print(f"6. {doc.reject('admin')}")
print(f" Status: {doc.get_status()}")
print(f"\nDocument history:")
for entry in doc.get_history():
print(f" {entry}")
# What we accomplished in this step:
# - Created complex document workflow with role-based permissions
# - Implemented conditional state transitions
# - Added history tracking for audit trails
# - Demonstrated real-world state pattern usage
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Understanding the State pattern and its benefits
# - Creating abstract state classes with common interfaces
# - Implementing concrete states with different behaviors
# - Building context classes that delegate to current state
# - Creating automatic state transitions
# - Implementing conditional transitions based on business rules
# - Adding history tracking and audit capabilities
#
# 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 creating a game character with different states!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================