Skip to content

Commit 893fcfd

Browse files
State Patterns
1 parent abb3910 commit 893fcfd

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# interesting but not practical :)
2+
from abc import ABC
3+
4+
5+
class Switch:
6+
def __init__(self):
7+
self.state = OffState()
8+
9+
def on(self):
10+
self.state.on(self)
11+
12+
def off(self):
13+
self.state.off(self)
14+
15+
16+
class State(ABC):
17+
def on(self, switch):
18+
print('Light is already on')
19+
20+
def off(self, switch):
21+
print('Light is already off')
22+
23+
24+
class OnState(State):
25+
def __init__(self):
26+
print('Light turned on')
27+
28+
def off(self, switch):
29+
print('Turning light off...')
30+
switch.state = OffState()
31+
32+
33+
class OffState(State):
34+
def __init__(self):
35+
print('Light turned off')
36+
37+
def on(self, switch):
38+
print('Turning light on...')
39+
switch.state = OnState()
40+
41+
42+
if __name__ == '__main__':
43+
sw = Switch()
44+
45+
sw.on() # Turning light on...
46+
# Light turned on
47+
48+
sw.off() # Turning light off...
49+
# Light turned off
50+
51+
sw.off() # Light is already off
52+
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from enum import Enum, auto
2+
3+
4+
class State(Enum):
5+
OFF_HOOK = auto()
6+
CONNECTING = auto()
7+
CONNECTED = auto()
8+
ON_HOLD = auto()
9+
ON_HOOK = auto()
10+
11+
12+
class Trigger(Enum):
13+
CALL_DIALED = auto()
14+
HUNG_UP = auto()
15+
CALL_CONNECTED = auto()
16+
PLACED_ON_HOLD = auto()
17+
TAKEN_OFF_HOLD = auto()
18+
LEFT_MESSAGE = auto()
19+
20+
21+
if __name__ == '__main__':
22+
rules = {
23+
State.OFF_HOOK: [
24+
(Trigger.CALL_DIALED, State.CONNECTING)
25+
],
26+
State.CONNECTING: [
27+
(Trigger.HUNG_UP, State.ON_HOOK),
28+
(Trigger.CALL_CONNECTED, State.CONNECTED)
29+
],
30+
State.CONNECTED: [
31+
(Trigger.LEFT_MESSAGE, State.ON_HOOK),
32+
(Trigger.HUNG_UP, State.ON_HOOK),
33+
(Trigger.PLACED_ON_HOLD, State.ON_HOLD)
34+
],
35+
State.ON_HOLD: [
36+
(Trigger.TAKEN_OFF_HOLD, State.CONNECTED),
37+
(Trigger.HUNG_UP, State.ON_HOOK)
38+
]
39+
}
40+
41+
state = State.OFF_HOOK
42+
exit_state = State.ON_HOOK
43+
44+
while state != exit_state:
45+
print(f'The phone is currently {state}')
46+
47+
for i in range(len(rules[state])):
48+
t = rules[state][i][0]
49+
print(f'{i}: {t}')
50+
51+
idx = int(input('Select a trigger:'))
52+
s = rules[state][idx][1]
53+
state = s
54+
55+
print('We are done using the phone.')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from enum import Enum, auto
2+
3+
4+
class State(Enum):
5+
LOCKED = auto()
6+
FAILED = auto()
7+
UNLOCKED = auto()
8+
9+
10+
if __name__ == '__main__':
11+
code = '1234'
12+
state = State.LOCKED
13+
entry = ''
14+
15+
while True:
16+
if state == State.LOCKED:
17+
entry += input(entry)
18+
19+
if entry == code:
20+
state = State.UNLOCKED
21+
22+
if not code.startswith(entry):
23+
# the code is wrong
24+
state = State.FAILED
25+
elif state == State.FAILED:
26+
print('\nFAILED')
27+
entry = ''
28+
state = State.LOCKED
29+
elif state == State.UNLOCKED:
30+
print('\nUNLOCKED')
31+
break

0 commit comments

Comments
 (0)