forked from tuvo1106/python_design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
64 lines (46 loc) · 1.43 KB
/
state.py
File metadata and controls
64 lines (46 loc) · 1.43 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
"""
State
- A pattern in which the object's behavior is determined by its state. An
object transitions from one state to another (something needs to trigger
a transition).
- A formalized construct which manages state and transitions is called a state
machine.
Motivation
- Consider an ordinary telephone
- What you do with it depends on the state of the phone/line
- If it's ringing or you want to make a call, you can pick it up
- Phone must be off the hook to talk/make a call
- If you try calling someone, and it's busy, you put the handset down
- Change in state can be explicit or in response to an event (Observer pattern)
"""
# interesting but not practical :)
from abc import ABC
class Switch:
def __init__(self):
self.state = OffState()
def on(self):
self.state.on(self)
def off(self):
self.state.off(self)
class State(ABC):
def on(self, switch):
print('Light is already on')
def off(self, switch):
print('Light is already off')
class OnState(State):
def __init__(self):
print('Light turned on')
def off(self, switch):
print('Turning light off...')
switch.state = OffState()
class OffState(State):
def __init__(self):
print('Light turned off')
def on(self, switch):
print('Turning light on...')
switch.state = OnState()
if __name__ == '__main__':
SW = Switch()
SW.on()
SW.off()
SW.off()