-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
51 lines (44 loc) · 1.26 KB
/
Copy pathcontext.py
File metadata and controls
51 lines (44 loc) · 1.26 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
from .state import State
class Context:
"""
The Context maintains a reference to a State instance that determines
its behavior.
"""
def __init__(self, state: State):
self._state: State = state
def change_state(self) -> None:
"""
Ask the current state what the next state should be.
"""
self._state = self._state.next_state()
def request(self) -> str:
"""
Delegate behavior to current state.
"""
return self._state.handle()
# Example Usage:
#
# from context import Context
# from concrete_state_a import ConcreteStateA
#
# context = Context(ConcreteStateA())
#
# print(context.request()) # "State A: Handling request."
# context.change_state()
#
# print(context.request()) # "State B: Handling request."
# context.change_state()
#
# print(context.request()) # "State A: Handling request."
#
#
# Common Mistakes to Avoid:
#
# 1. Hardcoding transitions inside Context.
# - Fix: Transitions belong inside State classes.
#
# 2. Using too many states when if/else would be simpler.
# - Fix: Use State Pattern only when behavior *must* change dynamically.
#
# 3. Exposing state-switching logic outside Context.
# - Fix: Provide methods like request() and change_state() only.