I was about to begin porting a relatively large game from iPhone Obj-C to a platform neutral C++. In our game, we made heavy use of the NSNotificationCenter
for notifying menus and game items of downloads and cache updates from our server.
I was thinking of re-creating the NSNotificationCenter
using a singleton class and then having any class which would receive notifications inherit a NotificationReceiver
base class using multi-inheritance. The singleton would keep a Map
of notification names that would keep an array of pointers to the base class. When the notification is sent, it would then call a virtual member in the base class of the receivers which could be overridden.
Is there a better design pattern to use in C++ for this type of operation?
Would there be a better way to implement this?
Would there be a better way to implement this?
Yes - there is no need to enforce inheriting a specific interface if you use e.g.:
std::function
in combination with Boost.Bind/std::bind
for the callbacksAlso keep in mind that if all you need is a single instance for the notification center, it doesn't need to be a singleton.