{"id":"6b0857af5a2625e44ecaad0a","slug":"notification-factory","title":"Notification factory","summary":"Implement Factory Method: every notification service decides which channel (e-mail, SMS, push) it creates.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":null,"status":"published","difficulty":"easy","topics":["factory-method"],"techs":["typescript","javascript"],"tags":["factory-method","notifications","creational"],"languages":[{"lang":"typescript","runner":"browser","starter":"interface Notification {\n  readonly channel: string;\n  send(to: string, text: string): string;\n}\n\nabstract class NotificationService {\n  /** Factory Method – every subclass decides which Notification it creates. */\n  protected abstract createNotification(): Notification;\n\n  notify(to: string, text: string): string {\n    if (!text.trim()) throw new Error('Empty message');\n    return this.createNotification().send(to, text);\n  }\n}\n\n// TODO: EmailNotification (\"email:<to>:<text>\"),\n//       SmsNotification (\"sms:<to>:<text>\", text cut to 160 characters),\n//       PushNotification (\"push:<to>:<text>\")\n\nclass EmailService extends NotificationService {\n  protected createNotification(): Notification {\n    throw new Error('Not implemented');\n  }\n}\n\n// TODO: SmsService, PushService\n\nfunction serviceFor(channel: string): NotificationService {\n  // TODO: 'email' | 'sms' | 'push' → the matching service, otherwise Error(\"Unknown channel: <channel>\")\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('EmailService sends e-mail notifications', () => {\n  expect(new EmailService().notify('ala@example.com', 'Hi')).toBe('email:ala@example.com:Hi');\n});\n\ntest('SmsService cuts the text to 160 characters', () => {\n  const result = new SmsService().notify('+48123', 'x'.repeat(200));\n  expect(result).toBe('sms:+48123:' + 'x'.repeat(160));\n});\n\ntest('PushService sends push notifications', () => {\n  expect(new PushService().notify('device-1', 'Hello')).toBe('push:device-1:Hello');\n});\n\ntest('services extend NotificationService', () => {\n  expect(new SmsService()).toBeInstanceOf(NotificationService);\n  expect(new PushService()).toBeInstanceOf(NotificationService);\n});\n\ntest('notify() uses the factory method of the subclass', () => {\n  class FakeService extends NotificationService {\n    createNotification() {\n      return { channel: 'fake', send: (to, text) => 'fake:' + to + ':' + text };\n    }\n  }\n  expect(new FakeService().notify('x', 'y')).toBe('fake:x:y');\n});\n\ntest('serviceFor() returns the matching service', () => {\n  expect(serviceFor('email')).toBeInstanceOf(EmailService);\n  expect(serviceFor('push')).toBeInstanceOf(PushService);\n});\n\ntest('serviceFor() rejects unknown channels', () => {\n  expect(() => serviceFor('fax')).toThrow('Unknown channel: fax');\n});\n"},{"lang":"javascript","runner":"browser","starter":"class NotificationService {\n  /** Factory Method – every subclass returns its own notification object { channel, send(to, text) }. */\n  createNotification() {\n    throw new Error('createNotification() must be overridden');\n  }\n\n  notify(to, text) {\n    if (!text.trim()) throw new Error('Empty message');\n    return this.createNotification().send(to, text);\n  }\n}\n\n// TODO: EmailNotification (\"email:<to>:<text>\"),\n//       SmsNotification (\"sms:<to>:<text>\", text cut to 160 characters),\n//       PushNotification (\"push:<to>:<text>\")\n\nclass EmailService extends NotificationService {\n  createNotification() {\n    throw new Error('Not implemented');\n  }\n}\n\n// TODO: SmsService, PushService\n\nfunction serviceFor(channel) {\n  // TODO: 'email' | 'sms' | 'push' → the matching service, otherwise Error(\"Unknown channel: <channel>\")\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('EmailService sends e-mail notifications', () => {\n  expect(new EmailService().notify('ala@example.com', 'Hi')).toBe('email:ala@example.com:Hi');\n});\n\ntest('SmsService cuts the text to 160 characters', () => {\n  const result = new SmsService().notify('+48123', 'x'.repeat(200));\n  expect(result).toBe('sms:+48123:' + 'x'.repeat(160));\n});\n\ntest('PushService sends push notifications', () => {\n  expect(new PushService().notify('device-1', 'Hello')).toBe('push:device-1:Hello');\n});\n\ntest('services extend NotificationService', () => {\n  expect(new SmsService()).toBeInstanceOf(NotificationService);\n  expect(new PushService()).toBeInstanceOf(NotificationService);\n});\n\ntest('notify() uses the factory method of the subclass', () => {\n  class FakeService extends NotificationService {\n    createNotification() {\n      return { channel: 'fake', send: (to, text) => 'fake:' + to + ':' + text };\n    }\n  }\n  expect(new FakeService().notify('x', 'y')).toBe('fake:x:y');\n});\n\ntest('serviceFor() returns the matching service', () => {\n  expect(serviceFor('email')).toBeInstanceOf(EmailService);\n  expect(serviceFor('push')).toBeInstanceOf(PushService);\n});\n\ntest('serviceFor() rejects unknown channels', () => {\n  expect(() => serviceFor('fax')).toThrow('Unknown channel: fax');\n});\n"}],"questionCount":null,"xp":10,"stats":{"attempts":1,"solves":1,"solutions":1,"comments":0},"solveRate":100,"publishedAt":"2026-11-26T16:00:47.675Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A shop sends notifications by e-mail, SMS and push. The base class "},{"type":"text","text":"NotificationService","marks":[{"type":"code"}]},{"type":"text","text":" already has "},{"type":"text","text":"notify()","marks":[{"type":"code"}]},{"type":"text","text":", but it does not know "},{"type":"text","text":"which","marks":[{"type":"bold"}]},{"type":"text","text":" notification to create – that is the job of the factory method "},{"type":"text","text":"createNotification()","marks":[{"type":"code"}]},{"type":"text","text":", overridden by subclasses."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"orderedList","attrs":{"start":1},"content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Write the notifications: "},{"type":"text","text":"EmailNotification","marks":[{"type":"code"}]},{"type":"text","text":" ("},{"type":"text","text":"email:<to>:<text>","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"SmsNotification","marks":[{"type":"code"}]},{"type":"text","text":" ("},{"type":"text","text":"sms:<to>:<text>","marks":[{"type":"code"}]},{"type":"text","text":", text cut to "},{"type":"text","text":"160 characters","marks":[{"type":"bold"}]},{"type":"text","text":") and "},{"type":"text","text":"PushNotification","marks":[{"type":"code"}]},{"type":"text","text":" ("},{"type":"text","text":"push:<to>:<text>","marks":[{"type":"code"}]},{"type":"text","text":")."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Write the services "},{"type":"text","text":"EmailService","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"SmsService","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"PushService","marks":[{"type":"code"}]},{"type":"text","text":" – each overrides "},{"type":"text","text":"createNotification()","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Add "},{"type":"text","text":"serviceFor(channel)","marks":[{"type":"code"}]},{"type":"text","text":" returning the matching service for "},{"type":"text","text":"'email' | 'sms' | 'push'","marks":[{"type":"code"}]},{"type":"text","text":" and throwing "},{"type":"text","text":"Error('Unknown channel: <channel>')","marks":[{"type":"code"}]},{"type":"text","text":" otherwise."}]}]}]},{"type":"callout","attrs":{"variant":"tip","title":"Why?"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Calling code only knows "},{"type":"text","text":"NotificationService","marks":[{"type":"code"}]},{"type":"text","text":". A new channel (say Slack) is a new pair of classes – "},{"type":"text","text":"notify()","marks":[{"type":"code"}]},{"type":"text","text":" stays untouched."}]}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[],"articles":[{"id":"6b08bd3480884893657cc96a","slug":"flyweight","title":"Flyweight – how to fit 60,000 stadium seats in memory"},{"id":"6b08bd3480884893657cc960","slug":"factory-method","title":"Factory Method – let the subclass decide what gets created"},{"id":"6b08bd2e80884893657cc8d4","slug":"abstract-factory","title":"Abstract Factory – whole families of objects that fit together"}],"topicsInfo":[{"slug":"factory-method","title":"Factory Method","group":"creational","icon":"factory"}],"author":{"id":"6b0857a15a2625e44ecaabec","name":"Anna Autorka","avatar":"/api/files/6b0857ec5a2625e44ecab0c6","handle":"anna-autorka","community":true},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-26T16:00:47.679Z","updatedAt":"2026-11-26T16:01:49.358Z"}