Singletons

This topic is 4877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I was wondering, is there any reason singletons are used like this:
public class Singleton
{
private Singleton () {}

private static Singleton _inst = null;

public Singleton Instance()
{
if (_inst == null)
_inst = new Singleton();

return _inst;
}

// Methods and such
public void DoSomething();
public int DoSomethingElse(int a);
}


Rather than a class with all static methods and member variables like this:
public class Singleton
{
private Singleton () {}

// Methods and such
public static void DoSomething();
public static int DoSomethingElse(int a);
}


As far as I can tell, they'd accomplish the same thing unless you need to do something nontrivial like loading resources from files when you get the instance for the first time. Otherwise it seems like the second one would be (somewhat trivially) easier to deal with because you don't have to call Singleton.Instance() to get the instance to use.

Share on other sites
I beleive it's because you cannot access non-static member variables from static functions.

Share on other sites
Quote:
 Original post by oshI beleive it's because you cannot access non-static member variables from static functions.

You could just make the member variables static too.

EDIT: But for some reasons, which Fruny describes, it might not be a good idea.

[Edited by - CTar on September 12, 2005 2:34:02 PM]

Share on other sites
A static local variable is initialized the first time the function is called. The order of initialization of static (global or class static members) variables across translation units is otherwise undefined. About the only exception would be, I think, global variables that are set to 0 or some other integer constant and whose value can directly be loaded from the binary image.

Using a static local gives you some certainty about when the variable is initialized: if you try to access it, it is initialized. In contrast, if you have a bunch of global variables relying on each other for initialization, you need to do some fancy footwork to make sure that all the globals a given variable relies on are initializes before itself.

In your example, a similar technique, using dynamic allocation is used, for the same end-result (modulo any coding errors [smile]).

Share on other sites
Here are a few reasons.
1. If A is your Singleton and B references A, there is no guarantee that A is initialized before B references it (as Fruny pointed out).
2. Your Singleton can't have virtual member functions.
3. A class derived from your Singleton, is not automatically a Singleton.

Share on other sites
Good thread [smile] I was just wondering the same thing.

Share on other sites
just thought this may fit in( bear with me as this may get a bit hairy ), working example of a singleton..

say your writing a game, and you have your sound class set up as a singleton, you are then able to call your sound class from anywhere in your code with
CAudioSystem::GetInstance()->PlaySound();

where if it was not a singleton class you would need to send in a pointer or already have one stored.

if this made any sense then i'll be happy

Share on other sites
Quote:
 Original post by NovaCainejust thought this may fit in( bear with me as this may get a bit hairy ), working example of a singleton..say your writing a game, and you have your sound class set up as a singleton, you are then able to call your sound class from anywhere in your code with*** Source Snippet Removed ***where if it was not a singleton class you would need to send in a pointer or already have one stored.if this made any sense then i'll be happy

This is a perfect example of how singletons are abused. A singleton is a creational pattern, not a structural pattern.

Share on other sites
so is it bad to use them in that manner? that was the way i was taught, but then again, my description didn't help any.

Share on other sites
If you need to use them in that manner, it's an indicator of bad code design. Consider: 99% of your objects should not be communicating with the sound system. It's not part of their responsibility. And one thing that maintains this is that they are not even told about the sound server. Having singletons encourages you to access the sound server just wherever you like, which leads to a strong dependency between the sound server and everything else.

Also, consider this scenario: You make your "game" class into a singleton, since you'll only have one game going at a time, and you might need to access it anywhere. 50,000 lines later, you decide you want to reuse the same codebase for the server, which can run multiple games simultaneously. Bad news: You now have to go back and rip out EVERY reference to the singleton, and completely redesign your class structure to account for the multiple Game objects.

A good example of something that SHOULD be a singleton is a Log class. It makes basic sense that you'll have only one Log (well, you may disagree on this point), and it also makes sense that you'd want to access the Log without explicitly passing it around. It is, in a sense, a class which is peripheral to the rest of the class structure. That's an ideal place for singletons.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 10
• 9
• 11
• Forum Statistics

• Total Topics
634096
• Total Posts
3015492
×