Loading...
To design a notification system, we must first understand how it will be used. The system should:
Based on the requirements, the main objects in the system include:
Channel class with common attributes and methods. Derive EmailChannel, SMSChannel, and PushChannel for channel-specific logic.Notification class and extend it for different types like TransactionalNotification and PromotionalNotification.Scheduler and RetryManager to ensure a single instance.class User:
def __init__(self, user_id, preferences):
self.user_id = user_id
self.preferences = preferences # dict of {channel: enabled/disabled}
class Notification:
def init(self, user, content, channel, scheduled_time=None):
self.user = user
self.content = content
self.channel = channel
self.scheduled_time = scheduled_time
self.status = "Pending"
class Channel:
def send(self, notification):
raise NotImplementedError
class EmailChannel(Channel):
def send(self, notification):
# Logic to send email
print(f"Sending email to {notification.user.user_id}")
class SMSChannel(Channel):
def send(self, notification):
# Logic to send SMS
print(f"Sending SMS to {notification.user.user_id}")
class Scheduler:
def schedule(self, notification):
# Add notification to a scheduling queue
pass
class RetryManager:
def retry(self, notification):
# Retry logic
pass
Channel class without modifying existing code.Channel class wherever required.Channel provide specific methods (send) that do not impose unnecessary functionality.NotificationService depend on abstractions like Channel rather than concrete implementations.The design can handle large-scale notifications by: