Generic observer pattern

In many cases in my application i need class A to register itself as a listener on class B to receive notification when something happens. In every case i define a separate interface B implements and A can call do. So for example, A will have the following method:

void registerSomeEventListener(SomeEventListener l);

Also, in many cases, B will need to support multiple listeners so i reimplement the registration and notifyAll logic.

One generic way i know is to have some EventListener (implement by A) and EventNotifier (implement by B) classes. In this case each event is identified by a string and A implements the method:

void eventNotified(string eventType);

I think this is not a good solution. It will result in many if-else statements in case A listens to several events and might result in bugs when event names are changed only in the listener or the notifier.

I wonder what is the correct way to implement the observer pattern in C++?
Topic archived. No new replies allowed.