Jump to content

- - - - -

Don't use Singletons for Event Managers!

4: Adsense

The most recent commit to the Hieroglyph 3 repository has reduced my usage of the simple event system in the engine. I think the reasons behind this can apply to others, so I wanted to make a post here about it and try to share my experiences with the community.

A long time ago, I added an event manager to the Hieroglyph codebase. This was really long time ago - something like 10 years ago... Anyway, it was added when I was still in my C++ infancy, and of course I chose to make the event manager a singleton. It was perfectly logical - why in the world would anyone want an event system where not every listener would receive all the messages it signed up for?

Naturally, my coding style has evolved along with time using the language. And naturally I developed a scenario where the event system would need to be replicated in several small mini-application systems (if you missed the last post, I am referring to my Glyphlets!). So I thought, I can just modify the event manager to not be a singleton anymore, and create them as standard objects for each glyphlet.

That would work fine, as long as I didn't make any assumptions in the code that there was only one event manager. Which I didn't do - the event listeners would grab a reference to the singleton at their startup, so essentially all of the event based functionality broke in a seemingly incoherent mess :(

So to dig myself out, I started to review all of the classes that were using the event manager at all. As it turns out, most of them didn't really need to be using events, and I modified them accordingly to use other mechanisms to achieve their messaging. At this point, I am now down to only having applications, Glyphlets (which are mini applications), and the camera classes using the event system. With the breadth of the issue reduced, I can now eliminate the singleton interface method and deal with the minimized problem in a comprehensible way.

The moral of the story is this - if you think you need a singleton, then implement the class as a normal object and create a single instance of it to use. Enforce the singleton on yourself, but don't mechanize it through the class. This way, you still respect the class' potential for being used in multiple instances, and it can be very easy to expand later on. By using the singleton interface, you limit future design decisions without much gain - at least in my view, it is better to trust yourself to follow the access rules than to force yourself into one design later on down the road!

Feb 22 2013 11:11 AM

Hopefully I'll get some time this weekend to turn my demo into a glyphlet :¬)

Feb 22 2013 07:28 PM

Hopefully I'll get some time this weekend to turn my demo into a glyphlet :¬)


You better!  I'm looking forward to seeing it running :)  Actually, that reminds me that I need to check out your latest changes to the demo too...

Feb 23 2013 01:37 AM

Live, learn, and write some more code. :)

Feb 23 2013 03:48 PM

Live, learn, and write some more code. smile.png


That sounds good - and indeed that's what I plan to do!

Note: GameDev.net moderates comments.