Search⌘ K
AI Features

Solution: Implement a Config-Driven Notification Service Factory

Explore how to centralize object creation using the factory pattern to dynamically produce notification services based on runtime config. Learn to implement a createNotificationService function that returns the correct service instance without conditionals and handles unsupported channels with clear errors.

Solution explanation

  • Lines 1–17: We define three interchangeable notification service classes: EmailService, SMSService, and PushService.

    • Each class exposes a .send() method, ensuring all services follow the same interface.

    • EmailService simulates sending emails, SMSService handles text messages, and PushService represents push notifications.

  • Lines 19–23: The ...