Singleton pattern
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.
There are some who are critical of the singleton pattern and consider it to be an anti-pattern in that it is frequently used in scenarios where it is not beneficial, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.[1][2][3]
Common uses
- The abstract factory, builder, and prototype patterns can use Singletons in their implementation.
- Facade objects are often singletons because only one Facade object is required.
- State objects are often singletons.
- Singletons are often preferred to global variables because:
Implementation
An implementation of the singleton pattern must:
- ensure that only one instance of the singleton class ever exists; and
- provide global access to that instance.
Typically, this is done by:
- declaring all constructors of the class to be private; and
- providing a static method that returns a reference to the instance.
The instance is usually stored as a private static variable; the instance is created when the variable is initialized, at some point before the static method is first called. The following is a sample implementation written in Java.
public final class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
Lazy initialization
A singleton implementation may use lazy initialization, where the instance is created when the static method is first invoked. If the static method might be called from multiple threads simultaneously, measures may need to be taken to prevent race conditions that could result in the creation of multiple instances of the class. The following is a thread-safe sample implementation, using lazy initialization, written in Java.
public final class Singleton {
private static Singleton instance = null;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) instance = new Singleton();
return instance;
}
}
References
- ↑ Scott Densmore. Why singletons are evil, May 2004
- ↑ Steve Yegge. Singletons considered stupid, September 2004
- ↑ Clean Code Talks - Global State and Singletons
- ↑ Gamma, E, Helm, R, Johnson, R, Vlissides, J: "Design Patterns", page 128. Addison-Wesley, 1995
External links
The Wikibook Computer Science/Design Patterns has a page on the topic of: Singleton implementations in various languages |
Wikimedia Commons has media related to Singleton pattern. |
- Complete article "Singleton Design Pattern techniques"
- 4 different ways to implement singleton in Java "Ways to implement singleton in Java"
- Book extract: Implementing the Singleton Pattern in C# by Jon Skeet
- Singleton at Microsoft patterns & practices Developer Center
- IBM article "Double-checked locking and the Singleton pattern" by Peter Haggar
- IBM article "Use your singletons wisely" by J. B. Rainsberger
- Javaworld article "Simply Singleton" by David Geary
- Google article "Why Singletons Are Controversial"
- Google Singleton Detector (analyzes Java bytecode to detect singletons)