44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
import abc
|
|
|
|
|
|
class State(object, metaclass=abc.ABCMeta):
|
|
def __init__(self):
|
|
self.color_index = 0
|
|
|
|
@abc.abstractmethod
|
|
def light(self):
|
|
COLORS = ["Green", "Red", "Blue", "Yellow"]
|
|
state_color = COLORS[self.color_index]
|
|
self.color_index += 1
|
|
if self.color_index == len(COLORS):
|
|
self.color_index = 0
|
|
return state_color
|
|
|
|
|
|
class Lamp(State):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.color = None
|
|
|
|
def light(self):
|
|
self.color = super().light()
|
|
return self.color
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
|
|
|
lamp_1 = Lamp()
|
|
lamp_2 = Lamp()
|
|
|
|
lamp_1.light() # Green
|
|
lamp_1.light() # Red
|
|
lamp_2.light() # Green
|
|
|
|
assert lamp_1.light() == "Blue"
|
|
assert lamp_1.light() == "Yellow"
|
|
assert lamp_1.light() == "Green"
|
|
assert lamp_2.light() == "Red"
|
|
assert lamp_2.light() == "Blue"
|
|
print("Coding complete? Let's try tests!")
|