From dc55183b16a7cb7b8ed494a17f3f441b7d493b94 Mon Sep 17 00:00:00 2001 From: sandsunset Date: Sat, 5 Apr 2025 23:00:32 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=EC=A0=84=EB=9E=B5=ED=8C=A8?= =?UTF-8?q?=ED=84=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- design_pattern_practice/strategy_pattern.py | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 design_pattern_practice/strategy_pattern.py diff --git a/design_pattern_practice/strategy_pattern.py b/design_pattern_practice/strategy_pattern.py new file mode 100644 index 0000000..3ba5587 --- /dev/null +++ b/design_pattern_practice/strategy_pattern.py @@ -0,0 +1,44 @@ +from abc import ABC, abstractmethod + + +class Notifier(ABC): + @abstractmethod + def notify(self, recipient: str, message: str) -> None: + pass + + +class EmailNotifier(Notifier): + def notify(self, recipient, message): + print(message) + + +class PhoneNotifier(Notifier): + def notify(self, recipient, message): + print(message) + + +class Product(): + def __init__(self, name: str, owner: str): + self.name = name + self.owner = owner + + +class OrderService: + def __init__(self, notifier: Notifier): + self.notifier = notifier + + def set_notifier(self, notifier: Notifier): + self.notifier = notifier + + def order(self, product: Product, quantity: int): + self.notifier.notify(product.owner, f"[주문알림] \"{product.name}\" 상품 {quantity}개 주문이 들어왔습니다") + + +if __name__ == "__main__": + onion_chicken = Product("마늘 치킨", "삼성통닭") + + order_service = OrderService(PhoneNotifier()) + order_service.order(onion_chicken, 2) + + order_service.set_notifier(EmailNotifier()) + order_service.order(onion_chicken, 5)