커맨드 패턴

From IT Wiki
Revision as of 17:23, 20 October 2021 by 126togo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

파이썬 코드 예제

  from abc import ABCMeta, abstractmethod
  class Order(metaclass=ABCMeta):
     @abstractmethod
     def execute(self):
         pass
  class BuyStockOrder(Order):
     def __init__(self, stock):
         self.stock = stock
     def execute(self):
         self.stock.buy()
  class SellStockOrder(Order):
     def __init__(self, stock):
         self.stock = stock
     def execute(self):
         self.stock.sell()
  class StockTrade:
     def buy(self):
         print(You will buy stocks)
     def sell(self):
         print(You will sell stocks)
  class Agent:
     def __init__(self):
         self.__orderQueue = []
     def placeOrder(self, order):
         self.__orderQueue.append(order)
         order.execute()
  if __name__ == __main__:
     #Client
     stock = StockTrade()
     buyStock = BuyStockOrder(stock)
     sellStock = SellStockOrder(stock)
     #Invoker
     agent = Agent()
     agent.placeOrder(buyStock)
     agent.placeOrder(sellStock)

참고 문헌