-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
48 lines (34 loc) · 927 Bytes
/
state.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
from __future__ import annotations
class State:
def __init__(self, tv: TV):
self.tv = tv
self.status = None
def operate(self):
print(f'Turning TV {self.status}')
class TurnOn(State):
def __init__(self, tv):
super().__init__(tv)
self.status = 'On'
def change_state(self):
print('Changing state to On...')
self.tv.state = self.tv.off
class TurnOff(State):
def __init__(self, tv):
super().__init__(tv)
self.status = 'Off'
def change_state(self):
print('Changing state to Off...')
self.tv.state = self.tv.on
class TV:
def __init__(self):
self.on = TurnOn(self)
self.off = TurnOff(self)
self.state = self.on
def press(self):
self.state.operate()
self.state.change_state()
if __name__ == '__main__':
tv = TV()
tv.press()
tv.press()
tv.press()