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.Channel class with specific implementations for EmailChannel, SMSChannel, PushChannel, etc.Notification class for different notification types.class User:
def __init__(self, user_id, preferences, time_zone):
self.user_id = user_id
self.preferences = preferences # {channel: enabled/disabled}
self.time_zone = time_zone
def get_preferences(self):
# Simulate fetching preferences from a database
return self.preferences
class Notification:
def init(self, user, content, channels, priority="Normal"):
self.user = user
self.content = content
self.channels = channels
self.priority = priority
self.status = "Pending"
self.attempts = 0
def update_status(self, new_status):
self.status = new_status
class Scheduler:
def schedule(self, notification):
# Convert user time zone to UTC for scheduling
utc_time = self.convert_to_utc(notification.user.time_zone, notification.scheduled_time)
# Add to a job queue (e.g., RabbitMQ)
print(f"Scheduled notification at {utc_time} for user {notification.user.user_id}")
def convert_to_utc(self, time_zone, time):
# Example conversion logic
return time # Simplified for demonstration
class RetryManager:
def retry(self, notification):
if notification.attempts >= 3:
notification.update_status("Failed")
Logger.log_error(f"Notification failed after retries: {notification}")
return
notification.attempts += 1
# Exponential backoff
delay = 2 ** notification.attempts
print(f"Retrying in {delay} seconds for user {notification.user.user_id}")
class Logger:
@staticmethod
def log_error(message):
# Write to an error log
print(f"ERROR: {message}")
@staticmethod
def log_event(message):
# Write to an event log
print(f"EVENT: {message}")
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:
user_id + content_hash).