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.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 1–17: We define three interchangeable notification service classes:
EmailService,SMSService, andPushService.Each class exposes a
.send()method, ensuring all services follow the same interface.EmailServicesimulates sending emails,SMSServicehandles text messages, andPushServicerepresents push notifications.
Lines 19–23: The ...