chain of responsibility 패턴

김대익·2022년 3월 31일
0

각각의 class들을 switch case문처럼 자신에게 맞는 case인지 차례대로 확인해가며 실행되는 패턴

class Handler:
  def __init__(self):
    self.next_handler = None
  def setNext(self,handler):
    self.next_handler = handler
  def handle(self,req):
    if self.next_handler:
      return self.next_handler.handle(req)
    print("All handlers failed")
    return None

class CashHandler(Handler):
  def handle(self,req):
    if req['method'] == 'cash':
      print(f"processing cash {req['amount']} won")
    else:
      print(f"CashHandler cannot process")
      super().handle(req)

class CreditCardHandler(Handler):
  def handle(self,req):
    if req['method'] == 'creditCard':
      print(f"processing creditCard {req['amount']} won")
    else:
      print(f"CreditCardHandler cannot process")
      super().handle(req)

class DebitCardHandler(Handler):
  def handle(self,req):
    if req['method'] == 'debitCard':
      print(f"processing debitCard {req['amount']} won")
    else:
      print(f"DebitCardHandler cannot process")
      super().handle(req)
      
cash_handler = CashHandler()
creditcard_handler = CreditCardHandler()
debitcard_handler = DebitCardHandler()

cash_handler.setNext(creditcard_handler)
creditcard_handler.setNext(debitcard_handler)

payment = {
  "method": "paypal",
  "amount": 10000
}

cash_handler.handle(payment)

0개의 댓글