중재자 패턴

김대익·2022년 4월 4일
0


class Mediator:
  def notify(sender):
    pass    

class Clock:
  def setMediator(self,mediator:Mediator):
    self.mediator = mediator
  def Alarm(self):
    print("alarm on")
    self.mediator.notify('AlarmOn')

class Light:
  def setMediator(self,mediator:Mediator):
    self.mediator = mediator
  def On(self):
    print("light On")    
  def Off(self):
    print("light off")
    self.mediator.notify('LightOff')

class Speaker:
  def setMediator(self,mediator:Mediator):
    self.mediator = mediator
  def On(self):
    print("speaker on")
  def Off(self):
    print("speaker off")

class HomeMediator(Mediator):
  def __init__(self,clock,light,speaker):
    self.clock = clock    
    self.light = light
    self.speaker = speaker

  def notify(self,signal:str):
    if signal == 'AlarmOn':
      self.speaker.On()
      self.light.On()

    elif signal == 'LightOff':
      self.speaker.Off()
  

0개의 댓글