singleton pattern

I've heard many people say the singleton pattern is evil. In my games I make my visualisation class a singleton class, so that all of my enemies, bullets, scenery etc. can call Draw() without me having to pass a pointer to the instance of visualisation to every single one of them.

Is this really bad? I don't see much of a downside, given that I'd have to pass the pointer to every entity anyway. Examples of situations in which a singleton could lead to problems would be greatly appreciated.

Thanks.
Singletons are evil for all the same reasons globals are evil. Any code you write that uses them will only work with that one object. It destroys modularity.

I'm not 100% sure what your Visualisation class is meant to do in your program, but by making it a singleton you are destroying the possibility of adding multiple visualisations at a later time (for instance if you add another player or an alternative way to view things or whatever)

Even "Game" classes that represent the game as a whole are better off not being a singleton because it can allow for multiple games to run simultaneously in the same exe. That may sound ridiculous, but it could be very useful for things like movie recording/playback, savestating, etc.


That's not to say that singletons are always back (just usually bad). Resource managers work very well as singletons.
Last edited on
Topic archived. No new replies allowed.