Polski

Notification factory

Implement Factory Method: every notification service decides which channel (e-mail, SMS, push) it creates.

easy · typescript · javascript · 10 XP

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

  1. Write the notifications: EmailNotification (email:<to>:<text>), SmsNotification (sms:<to>:<text>, text cut to 160 characters) and PushNotification (push:<to>:<text>).
  2. Write the services EmailService, SmsService, PushService – each overrides createNotification().
  3. Add serviceFor(channel) returning the matching service for 'email' | 'sms' | 'push' and throwing Error('Unknown channel: <channel>') otherwise.

Topics: Factory Method