state 패턴

김대익·2022년 3월 31일
0

절차에 따라 상태들을 바꿔가며 넣는 패턴이다

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())

0개의 댓글