I\'ve tried a few times to understand what a Singleton is. Perhaps I\'m just too visual.. so can anyone break it down in a simple analogy.
Similar Posts:
Besides its place as a particular design pattern, singletons can also be thought of as simply one thing. Its use in programming no doubt derives from its use in mathematics, where a singleton is a set of just one number.
A singleton is a class of which there can be only one instance in your application. You then share that instance throughout your application.
Here's a link that might help (covers how to make your singleton thread safe in c# as well):
Implementing the Singleton Pattern in C#
There was an excellent Google Tech Talk where the speaker addressed global state (including Singletons) and why they make testing nearly impossible:
The Clean Code Talks - "Global State and Singletons"
The speaker makes a distinction between what he calls Singletons (capital 'S'), where steps are taken to prevent the class from being instantiated more than once (i.e., often with a static getInstance
method and private constructors), and singletons (small 's') where a single instance is all that is ever needed, but nothing is built into the class to prevent multiple instances from being created.
Singleton is useful when you must be sure that there is one and only one instance of a class, and that this object must be accessed from multiple locations in the code.
If it could make sense that more than one instance of your class could be used at once, then you don't want a singleton.
Here is some information about where to use singletons: http://www.ibm.com/developerworks/webservices/library/co-single.html
From the article mentioned previously:
To decide whether a class is truly a singleton, you must ask yourself some questions.
- Will every application use this class exactly the same way? (exactly is the key word)
- Will every application ever need only one instance of this class?
(ever and one are the key words)Should the clients of this class be unaware of the application they are
part of?If you answer yes to all three questions, then you've found a singleton. The key points here are that a class is only a singleton if all applications treat it exactly the same and if its clients can use the class without an application context.
As requested, here are a few analogies:
You could instantiate lots of True objects, but they will all refer to the same actual item (i.e. the universal concept of True). Depending on your application's domain, there may be more specific examples:
A singleton is a class, that can only have a single instance created.
This is often implemented by having a private constructor, which is assigned to a static variable, and then accessible through a public method.
There are some problems with using singletons, including:
See here for a further description of singleton, and another pattern 'Monostate' that might be useful instead: http://www.objectmentor.com/resources/articles/SingletonAndMonostate.pdf