-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1_classic.py
53 lines (33 loc) · 1011 Bytes
/
1_classic.py
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
# 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() # Turning light on...
# Light turned on
sw.off() # Turning light off...
# Light turned off
sw.off() # Light is already off