Notification factory
Implement Factory Method: every notification service decides which channel (e-mail, SMS, push) it creates.
A shop sends notifications by e-mail, SMS and push. The base class NotificationService already has notify(), but it does not know which notification to create – that is the job of the factory method createNotification(), overridden by subclasses.
Task
- Write the notifications:
EmailNotification(email:<to>:<text>),SmsNotification(sms:<to>:<text>, text cut to 160 characters) andPushNotification(push:<to>:<text>). - Write the services
EmailService,SmsService,PushService– each overridescreateNotification(). - Add
serviceFor(channel)returning the matching service for'email' | 'sms' | 'push'and throwingError('Unknown channel: <channel>')otherwise.
Topics: Factory Method