커맨드 패턴: Difference between revisions

From IT Wiki
(새 문서: * 상위 문서: 소프트웨어 디자인 패턴 == 파이썬 코드 예제 == <syntaxhighlight lang="python3" line="1"> from abc import ABCMeta, abstractmeth...)
 
No edit summary
 
Line 39: Line 39:
     agent.placeOrder(sellStock)
     agent.placeOrder(sellStock)
</syntaxhighlight>
</syntaxhighlight>
== 참고 문헌 ==
* [https://q.fran.kr/%EB%AC%B8%EC%A0%9C/14464 국가공무원 7급 소프트웨어 공학 2020년 기출문제]
[[분류:소프트웨어 공학]]
[[분류:소프트웨어 공학]]

Latest revision as of 17:23, 20 October 2021

파이썬 코드 예제[edit | edit source]

  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)

참고 문헌[edit | edit source]