Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 2.5 KB

File metadata and controls

71 lines (54 loc) · 2.5 KB

Lab 10 — Notification Channels (Strategy Pattern)

Goal

Practice designing a small interface hierarchy around the Strategy pattern, where an outer-facing class knows nothing about which concrete implementation it's driving.

Description

NotificationService sends alerts without knowing how they're delivered: it's constructed with a pointer to an AbstractChannel and just forwards alert(target, message) to that channel's send().

Channel hierarchy:

AbstractChannel (interface: send(target, message))
    |-- SmsChannel        — prints "SMS to {target}: {message}"
    |-- HttpChannel        — shared HTTP-request formatting/printing
    |       |-- SlackChannel — POSTs to slack.example, payload "channel=...&message=..."
    |       |-- TeamsChannel — POSTs to teams.example, payload "group=...&text=..."
    |-- MultiChannel      — fans a single send() out to every registered channel

HttpChannel implements send() once (formats and prints the request line) and defers the two things that differ per service — the target url() and the payload() string — to private virtual hooks overridden by SlackChannel/TeamsChannel. MultiChannel is itself an AbstractChannel that holds non-owning pointers to other channels and broadcasts to all of them.

Every object here lives for the scope of main — none of the channels are heap-allocated, so there's nothing to manually clean up.

Project structure

├── include/
│   ├── AbstractChannel.h
│   ├── HttpChannel.h
│   ├── SlackChannel.h
│   ├── TeamsChannel.h
│   ├── SmsChannel.h
│   ├── MultiChannel.h
│   └── NotificationService.h
└── src/main.cpp

Requirements

  1. NotificationService depends only on AbstractChannel, never on a concrete channel type.
  2. SlackChannel/TeamsChannel share their HTTP-sending logic through HttpChannel and only override the URL and payload formatting.
  3. MultiChannel forwards every send() call to all channels added via addChannel().

Expected output

SMS to +48123123123: Your code is 1234

HTTP request to https://slack.example/send: channel=#backend&message=Deployment completed

HTTP request to https://teams.example/send: group=dev-team&text=Meeting starts in 5 minutes

SMS to global-alert: Production is down
HTTP request to https://slack.example/send: channel=global-alert&message=Production is down
HTTP request to https://teams.example/send: group=global-alert&text=Production is down