Advanced OOP and patterns · 11 / 15 Exercise Hard

Event system (Observer)

Objective

In the Observer pattern some objects (subscribers) register to be notified when another one (the emitter) publishes an event. The emitter does not know who they are or what they do: it only walks its list.

Instructions

  • The first line has the number n of commands. Each one is subscribe name event, unsubscribe name event or emit event data (the data may have several words).
  • When emitting, notify the subscribers of that event in the order they subscribed, showing name receives event: data for each one; if nobody listens, show Nobody listens to event. Subscribing twice to the same event does not duplicate the notice.
Input
4
subscribe ana news
subscribe bob news
emit news hola
emit sport gol
Output
ana receives news: hola
bob receives news: hola
Nobody listens to sport

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise