Singletons

Published January 15, 2011
Advertisement
[size="4"][DEVELOPMENT: Archetype Engine]

I hem and haw over these often. Should I use them? Are the necessary? Generally, though, I've come to the conclusion that I need them for Archetype. There are a few components that should not be duplicated in more than once instance. For example, the display class should only be created once. We only need ONE window (and, as far as I can tell, can only create one window), so there should never be any confusion about there being a single one of these objects.

A better argument for my use of singletons, however, is in my events class. I want to be able to access my events class in all objects that need to receive or transmit events, but I want to be able to do it without any object needing to know where to get access to it. So, the question becomes, how do I give my objects access to the global events system, without building into my objects some information about the structure of my engine (and in so doing, causing a breakdown in the reusability of my code)? The singleton has always come to me as the answer to this issue. For each object that needs to send and/or receive events, I can allow them to "create" an event object, send or bind to an event, and I'm done! The beauty of the singleton is, when I "create" an event object, all I'm doing is obtaining a reference to an already existing instance. I make one change to that instance from any object within the game and it's changed system wide! Being that simple, I keep coming back to rely on singletons.

Of course, they also give me a little worry. I haven't even touched threading yet, and there may be some issues that arise when using singletons in a threaded environment. But, for the most part, that's not an issue yet.

So anyway... I've added singletons to the Archetype engine, and, while I've used singletons in other projects, I've come across an implementation which I've been "borrowing" heavily from for my current singleton implementation. For anyone interested, http://www.garyrobinson.net/2004/03/python_singleto.html

[size="4"][RAMBLINGS]

So, ummm... yeah... I'm experimenting with how to organize my journal posts (Yeah... I like the term "journal" over blog). Let's see how this evolves.

GAH! CT is going to get ANOTHER "big storm" in a couple of days. We've got three feet of this white crap in our back yards already... we don't need more! This winter is going to be brutal! If the snow doesn't stop, I'll be spending more time passed out from shoveling than I will be sitting here working on my engine! *Takes a deeeeeep breath*

HEY! I'd also like to thank everyone who's commented so far, and who's been reading my journal. Hope it's been worth the read for you all!!
Previous Entry I cheated...
0 likes 4 comments

Comments

Saruman
Singletons are one of the worst anti-patterns I've seen and the majority of the time are usually used to patch up a broken architecture. So I have a few comments:

1.) If you only need one display class and one window.... only create one of each. The singleton pattern states that it is an object that is only created once AND global access is needed. Does your sound system need access to the window... or the display class? Usually having these as singletons just creates spaghetti-like access from various classes when a design issue creeps up (and it will), rather than sitting and thinking through the design and reworking some things most people will just call the singleton and "change it later".

2.) The worst thing you can have when dealing with threaded systems are global static variables, they are an extreme no-no. So I would really suggest thinking over how to architect your system without using global static variables, as this completely blows up threading/concurrency and you *will* have to rewrite it.
January 15, 2011 05:48 AM
ObsidianBlk
[quote name='Saruman' timestamp='1295070489']
Singletons are one of the worst anti-patterns I've seen and the majority of the time are usually used to patch up a broken architecture. So I have a few comments:

1.) If you only need one display class and one window.... only create one of each. The singleton pattern states that it is an object that is only created once AND global access is needed. Does your sound system need access to the window... or the display class? Usually having these as singletons just creates spaghetti-like access from various classes when a design issue creeps up (and it will), rather than sitting and thinking through the design and reworking some things most people will just call the singleton and "change it later".

2.) The worst thing you can have when dealing with threaded systems are global static variables, they are an extreme no-no. So I would really suggest thinking over how to architect your system without using global static variables, as this completely blows up threading/concurrency and you *will* have to rewrite it.
[/quote]

Ok... You do have a couple of valid points. You're right, my display class doesn't need to be a singleton, technically, and, using your example, no, my sound system wouldn't need access to my display class. That said, however, what about the events system? The events system should ABSOLUTELY only be created once, and, global-like access to it would be needed as my display class, my sound system, and various other class objects would need access to the events system for sending and receiving event messages.

I'm more than willing to concede that the singleton is not the way to go. As I stated in my post, I have hemmed and hawed over using it on many occasions (though, on the occasions I did use it, it worked without issue... non-threaded small projects). But if I shouldn't be using a singleton, any suggestions regarding a method in which all objects that need access to the events system get access to the events system without having to explicitly give each object a reference to the event system object?
January 15, 2011 02:37 PM
yckx
[quote name='ObsidianBlk' timestamp='1295102221']
But if I shouldn't be using a singleton, any suggestions regarding a method in which all objects that need access to the events system get access to the events system without having to explicitly give each object a reference to the event system object?
[/quote]
I've stopped using singletons, but this is the big failure I've always seen in anti-singleton arguments. Saying "You should use better design" is not helpful in itself, and I've never come across any more concrete singleton-avoidance or -alternative advice.
January 15, 2011 04:26 PM
ObsidianBlk
[quote name='yckx' timestamp='1295108779']
[quote name='ObsidianBlk' timestamp='1295102221']
But if I shouldn't be using a singleton, any suggestions regarding a method in which all objects that need access to the events system get access to the events system without having to explicitly give each object a reference to the event system object?
[/quote]
I've stopped using singletons, but this is the big failure I've always seen in anti-singleton arguments. Saying "You should use better design" is not helpful in itself, and I've never come across any more concrete singleton-avoidance or -alternative advice.
[/quote]


Yeah, I have to agree. I've encountered the "don't use singletons" argument on other forum posts, and sites here and across the web, but haven't heard of an alternative. The problem is, I want to give all my engine objects access to the events system (if they need it), but don't want to give every class (whether directly or via inheritance) a permanent reference object.

I'm open to any alternative solutions anyone may have, but, until I come across a better method, I'm going to be using singletons... for at least the events system, if nothing else.
January 15, 2011 04:57 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement