Design a Notification System
You'll learn to
- -Use Observer so the code that raises an event never knows which channels exist
- -Give each channel its own formatter, and isolate failures so one broken channel cannot block the rest
- -Add per-user opt-outs by wrapping a channel (a decorator) instead of branching inside the core loop
When you buy something online you may get an email, a text message and a phone alert about the same shipment. The code that ships the order does not decide which of those happen. A notification system is close to a pure application of the Observer pattern. Working it through is mostly about layering realistic details around that core, such as per-channel wording, delivery failures and user preferences, without letting them leak into each other.
Step 1: The Observer Core
An event is something that happened (an order shipped, a comment was posted). A channel is one way of telling a person about it (email, SMS, push). The service holds a list of subscribed channels. Publishing an event hands it to each channel, and the publisher knows nothing about how any channel works.
class NotificationEvent:
def __init__(self, event_type: str, user_id: str, data: dict):
self.event_type = event_type
self.user_id = user_id
self.data = data
class NotificationChannel(ABC):
@abstractmethod
def send(self, event: NotificationEvent) -> None: ...
class NotificationService:
def __init__(self):
self._channels: list[NotificationChannel] = []
def subscribe(self, channel: NotificationChannel) -> None:
self._channels.append(channel)
def publish(self, event: NotificationEvent) -> None:
for channel in self._channels:
channel.send(event) # publisher has zero knowledge of channel internalsStep 2: Per-Channel Formatting as a Strategy
An order-shipped event reads very differently in each place. An email has a subject line and a longer friendly body. An SMS is one short line. A push notification has a title and a shortened body. Each channel owns a formatter, so wording stays with the channel that needs it, not in one sprawling class that knows every format.
class MessageFormatter(ABC):
@abstractmethod
def format(self, event: NotificationEvent) -> str: ...
class SMSFormatter(MessageFormatter):
def format(self, event: NotificationEvent) -> str:
return f"Update: {event.data.get('summary', event.event_type)}"
class EmailChannel(NotificationChannel):
def __init__(self, formatter: MessageFormatter):
self._formatter = formatter
def send(self, event: NotificationEvent) -> None:
body = self._formatter.format(event)
deliver_email(event.user_id, subject=event.event_type, body=body)Step 3: Retry and Failure Without Coupling
A delivery can fail, for example when an email provider times out. That failure must not block or corrupt delivery on the other channels. Wrap each send in its own try/except that records the failure and moves on. A channel can also retry with a growing pause between attempts (backoff) inside itself, so NotificationService needs to know nothing about retry policy.
class NotificationService:
def publish(self, event: NotificationEvent) -> None:
for channel in self._channels:
try:
channel.send(event)
except DeliveryError as e:
log_failure(channel, event, e) # isolated failure, loop continues
continueStep 4: User Preferences, Opt-Out per Channel
A likely follow-up is that users may turn off SMS and keep email. Rather than adding an if inside publish(), wrap the subscription. A decorator is an object that wraps another with the same interface and adds one behavior. A PreferenceFilteredChannel wraps any channel, checks the user's preferences, and only then hands over to the real channel. Preferences become one more layer, not a special case scattered through the core loop.
Step 5: Run It
The demo wires up email, a preference-filtered SMS channel, and a push channel whose provider is down. It then publishes the same event for two users, and the second user has opted out of SMS. Watch that the push failure does not stop anything else.
Say the fault-isolation detail out loud: "one channel's failure must never stop delivery through the others." It is the production-minded detail that separates a strong answer from a merely correct one on this prompt.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Forge Level 43: Notification System asks you to implement the Observer pattern - subscribe()/notify() fanning out to multiple channel types - graded by running your actual code against real test cases.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!