Exercise: Observer
This exercise puts the Observer pattern into code.
Scenario
A Publisher manages a registry of Subscriber objects. Each subscriber holds a name and
an update(msg) method that returns "<name> got <msg>". When the publisher receives a
message, it calls update on each subscriber in subscription order and collects the results.
Duplicate subscriptions are silently ignored; the original position is preserved.
Commands
| Command | Behavior | Output |
|---|---|---|
["subscribe", name] | Register the subscriber; ignore if already subscribed | (none) |
["unsubscribe", name] | Remove the subscriber if present; do nothing otherwise | (none) |
["publish", msg] | Notify every current subscriber in subscription order | one line per subscriber: "<name> got <msg>"; if no subscribers: "No subscribers" |
A single publish command can emit multiple output lines — one per active subscriber.
6 subscribe Alice subscribe Bob publish hello unsubscribe Bob subscribe Alice publish bye
Alice got hello Bob got hello Alice got bye
Your task
Implement the skeleton in the editor below so the commands produce the output described above.