Practice designing a small interface hierarchy around the Strategy pattern, where an outer-facing class knows nothing about which concrete implementation it's driving.
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.
├── include/
│ ├── AbstractChannel.h
│ ├── HttpChannel.h
│ ├── SlackChannel.h
│ ├── TeamsChannel.h
│ ├── SmsChannel.h
│ ├── MultiChannel.h
│ └── NotificationService.h
└── src/main.cpp
NotificationServicedepends only onAbstractChannel, never on a concrete channel type.SlackChannel/TeamsChannelshare their HTTP-sending logic throughHttpChanneland only override the URL and payload formatting.MultiChannelforwards everysend()call to all channels added viaaddChannel().
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