{"id":"6b0857af5a2625e44ecaad0a","slug":"notification-factory","title":"Fabryka powiadomień","summary":"Zaimplementuj Metodę wytwórczą: każdy serwis powiadomień sam decyduje, jaki kanał (e-mail, SMS, push) tworzy.","servedLang":"pl","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":"pl","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Sklep wysyła powiadomienia e-mailem, SMS-em i push. Klasa bazowa "},{"type":"text","text":"NotificationService","marks":[{"type":"code"}]},{"type":"text","text":" ma już gotową metodę "},{"type":"text","text":"notify()","marks":[{"type":"code"}]},{"type":"text","text":", ale nie wie, "},{"type":"text","text":"jakie","marks":[{"type":"bold"}]},{"type":"text","text":" powiadomienie utworzyć – to zadanie metody wytwórczej "},{"type":"text","text":"createNotification()","marks":[{"type":"code"}]},{"type":"text","text":", którą nadpisują podklasy."}]},{"type":"heading","attrs":{"level":2,"id":"zadanie"},"content":[{"type":"text","text":"Zadanie"}]},{"type":"orderedList","attrs":{"start":1},"content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Napisz klasy powiadomień: "},{"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":", tekst przycięty do "},{"type":"text","text":"160 znaków","marks":[{"type":"bold"}]},{"type":"text","text":") i "},{"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":"Napisz serwisy "},{"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":" – każdy nadpisuje "},{"type":"text","text":"createNotification()","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Dodaj funkcję "},{"type":"text","text":"serviceFor(channel)","marks":[{"type":"code"}]},{"type":"text","text":", która dla "},{"type":"text","text":"'email' | 'sms' | 'push'","marks":[{"type":"code"}]},{"type":"text","text":" zwraca właściwy serwis, a dla innego kanału rzuca "},{"type":"text","text":"Error('Unknown channel: <channel>')","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"callout","attrs":{"variant":"tip","title":"Dlaczego tak?"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Kod wywołujący pracuje tylko z "},{"type":"text","text":"NotificationService","marks":[{"type":"code"}]},{"type":"text","text":". Nowy kanał (np. Slack) to nowa para klas – bez zmiany "},{"type":"text","text":"notify()","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[],"articles":[{"id":"6b08bd3480884893657cc96a","slug":"flyweight","title":"Pyłek (Flyweight) – jak zmieścić w pamięci 60 tysięcy miejsc na stadionie"},{"id":"6b08bd3480884893657cc960","slug":"factory-method","title":"Metoda wytwórcza (Factory Method) – niech podklasa zdecyduje, co powstaje"},{"id":"6b08bd2e80884893657cc8d4","slug":"abstract-factory","title":"Fabryka abstrakcyjna (Abstract Factory) – całe rodziny obiektów, które do siebie pasują"}],"topicsInfo":[{"slug":"factory-method","title":"Metoda wytwórcza","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"}