-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
72 lines (55 loc) · 1.61 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab/
# --------------------------------------------------------
"""State"""
class LampStateBase:
"""Lamp status"""
def get_color(self):
raise NotImplementedError()
class GreenLampState(LampStateBase):
def get_color(self):
return 'Green'
class RedLampState(LampStateBase):
def get_color(self):
return 'Red'
class BlueLampState(LampStateBase):
def get_color(self):
return 'Blue'
class Lamp:
def __init__(self):
self._current_state = None
self._states = self.get_states()
@staticmethod
def get_states():
return [GreenLampState(), RedLampState(), BlueLampState()]
def next_state(self):
if self._current_state is None:
self._current_state = self._states[0]
else:
index = self._states.index(self._current_state)
if index < len(self._states) - 1:
index += 1
else:
index = 0
self._current_state = self._states[index]
return self._current_state
def light(self):
state = self.next_state()
print(state.get_color())
def main():
lamp = Lamp()
[lamp.light() for _ in range(3)]
# Green
# Red
# Blue
[lamp.light() for _ in range(3)]
# Green
# Red
# Blue
if __name__ == '__main__':
main()