Publish/Subscribe Implementation - opinions

I have taken a look at an example published by makomweb on gist. I have taken the liberty to add some comments here:

https://gist.github.com/jmjatlanta/36cb230f93126468783b59301574bc96

In this implementation, (we'll call it implementation #1) the Publisher saves a method to be called when an event happens. This makes the "Publish" method easy and elegant.

The magic happens in the Event.operator(). This is where the map of methods is iterated, and called.

An alternate implementation (we'll call it implementation #2) would be for any subscriber to comply to an interface called subscriber, and the Publisher having a vector of instances of subscriber.

My question is: Is one implementation better than the other? I mean, it is nice that in #1 the complexity is hidden. But the complexity is still there. It just moved.

I would argue that #2 would (potentially) be more readable, as the implementation is straightforward. It requires that a class implement an interface, but I would argue that #1 does too, albeit at the method level instead of the class level.

Hence, I ask the community. What do you think? Are there advantages of one over the other that are perhaps more subtle? I look forward to your opinions.
Topic archived. No new replies allowed.