Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions design_pattern_practice/strategy_pattern.py
Original file line number Diff line number Diff line change
@@ -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)