SDE Path

Notification Service (Strategy + Observer)

Medium

Notification Service (Strategy + Observer)

Per-user channel preferences with pluggable delivery channels — the design that combines Strategy and Observer in one clean class.

Requirements

  • Exactly three channels exist: EMAIL, SMS, PUSH — output order is always this fixed order regardless of enable order.
  • Users must be registered before preferences apply; operations on unknown users are silent no-ops except notify, which answers -.
  • Enable/disable are idempotent.
  • New users start with everything off (explicit opt-in — the compliant default).

API to implement

Implement the class Notifier. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | register <userId> | — | Create the user with all channels disabled. Implemented as the method register_user (register is a reserved word in C++). Re-registering an existing user is a no-op (preferences kept). | | enable <userId> <channel> | — | Enable EMAIL, SMS, or PUSH for the user. Unknown users: no-op. Idempotent. | | disable <userId> <channel> | — | Disable the channel (no-op if unknown user or already off). | | notify <userId> <message> | string | Deliver message on the user's enabled channels. Return the deliveries in fixed channel order EMAIL, SMS, PUSH, each formatted CHANNEL:message, joined by | — or - if the user is unknown or has nothing enabled. |

Input format

The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
8
register u1
notify u1 hi
enable u1 PUSH
enable u1 EMAIL
notify u1 sale
disable u1 EMAIL
notify u1 bye
notify ghost x
Expected output
-
EMAIL:sale|PUSH:sale
PUSH:bye
-
Example 2
Input
6
register a
enable a SMS
enable a SMS
notify a m1
register a
notify a m2
Expected output
SMS:m1
SMS:m2