절차에 따라 상태들을 바꿔가며 넣는 패턴이다
class TrafficLight:
def __init__(self):
self.state = GreenLight()
def setState(self,state):
self.state = state
def speak(self):
self.state.status()
def wait(self):
self.state.changeLight(self)
class State:
def status(self):
pass
def changeLight(self,traffic_light:TrafficLight):
pass
class GreenLight(State):
def status(self):
print('green light')
def changeLight(self,traffic_light:TrafficLight):
print('wait.. the light changed')
traffic_light.setState(RedLight())
class RedLight(State):
def status(self):
print('red light')
def changeLight(self,traffic_light:TrafficLight):
print('wait.. the light changed')
traffic_light.setState(GreenLight())