Search code examples
c++design-patternsobserver-patternnsnotificationcenter

Moving NSNotificationCenter Design Pattern to C++ for porting?


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?


Solution

  • 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.:

    Also keep in mind that if all you need is a single instance for the notification center, it doesn't need to be a singleton.