커맨드 패턴

IT위키

파이썬 코드 예제[편집]

 1   from abc import ABCMeta, abstractmethod
 2   class Order(metaclass=ABCMeta):
 3      @abstractmethod
 4      def execute(self):
 5          pass
 6   class BuyStockOrder(Order):
 7      def __init__(self, stock):
 8          self.stock = stock
 9      def execute(self):
10          self.stock.buy()
11   class SellStockOrder(Order):
12      def __init__(self, stock):
13          self.stock = stock
14      def execute(self):
15          self.stock.sell()
16   class StockTrade:
17      def buy(self):
18          print(You will buy stocks)
19      def sell(self):
20          print(You will sell stocks)
21   class Agent:
22      def __init__(self):
23          self.__orderQueue = []
24      def placeOrder(self, order):
25          self.__orderQueue.append(order)
26          order.execute()
27   if __name__ == __main__:
28      #Client
29      stock = StockTrade()
30      buyStock = BuyStockOrder(stock)
31      sellStock = SellStockOrder(stock)
32      #Invoker
33      agent = Agent()
34      agent.placeOrder(buyStock)
35      agent.placeOrder(sellStock)

참고 문헌[편집]