Notes and exercises for learning design patterns
You are building a notification system. The core object is a Notifier that sends messages to users:
class EmailNotifier(Notifier):
def send(self, recipient: str, message: str) -> None:
print(f"EMAIL to {recipient}: {message}")
The system works fine. But now two new requirements arrive:
You must add these behaviours without modifying EmailNotifier.
Implement LoggingDecorator. It should:
"SENDING notification to {recipient}" before the notification is sent."SENT notification to {recipient}" after the notification is sent.Expected output:
SENDING notification to alice@example.com
EMAIL to alice@example.com: Meeting at 3pm
SENT notification to alice@example.com
Implement UppercaseDecorator. It should:
message to uppercase before passing it to the wrapped notifier.recipient.Expected output when wrapping EmailNotifier directly:
EMAIL to bob@example.com: SERVER IS DOWN
Wrap EmailNotifier with both decorators so that the message is uppercased and the send is logged.
Expected output:
SENDING notification to bob@example.com
EMAIL to bob@example.com: SERVER IS DOWN
SENT notification to bob@example.com
See exercise1.py.