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.
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]).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
Quote:Original post by NovaCaine 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 *** 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.
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.