Another example ... for my own use I made an abstract publisher class. It provides methods to add and remove subscribers based on
string keys but no publish method. I make an interface for each message type that is to be published. The concrete publisher implements the interface, as do the subscribers. So a concrete publisher has something like this for each method defined in the interface:
This is more hand coding than you'll find using Observable and Observer - usually almost exactly the 6 lines you see above - but has some neat advantages. I can publish any method name with any arguments. One publisher can have many methods. I can put logic in the publisher to do things like accumulate responses from subscribers for voting or polling, stop after any subscriber returns a success or failure flag, return values from the publish method, etc.
Another interesting variation has a queue per subscriber and puts message objects into the queue instead of calling the subscriber directly. That lets the publisher and subscriber work asynchronously and works even if the subscriber doesn't exist at the moment.
There is lots of fun to be had in pub-sub for sure.
