Singleton pattern
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/Singleton_UML_class_diagram.svg/220px-Singleton_UML_class_diagram.svg.png)
In
More specifically, the singleton pattern allows objects to:[2]
- Ensure they only have one instance
- Provide easy access to that instance
- Control their instantiation (for example, hiding the constructors of a class)
The term comes from the mathematical concept of a singleton.
Common uses
Singletons are often preferred to
The singleton pattern can also be used as a basis for other design patterns, such as the abstract factory, factory method, builder and prototype patterns. Facade objects are also often singletons because only one facade object is required.
Implementations
Implementations of the singleton pattern ensure that only one instance of the singleton class ever exists and typically provide
Typically, this is accomplished by:
- Declaring all private, which prevents it from being instantiated by other objects
- Providing a static method that returns a referenceto 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 when the static method is first called.
This C++11 implementation is based on the pre C++98 implementation in the book [citation needed].
#include <iostream>
class Singleton {
public:
// defines an class operation that lets clients access its unique instance.
static Singleton& get() {
// may be responsible for creating its own unique instance.
if (nullptr == instance) instance = new Singleton;
return *instance;
}
Singleton(const Singleton&) = delete; // rule of three
Singleton& operator=(const Singleton&) = delete;
static void destruct() {
delete instance;
instance = nullptr;
}
// existing interface goes here
int getValue() {
return value;
}
void setValue(int value_) {
value = value_;
}
private:
Singleton() = default; // no public constructor
~Singleton() = default; // no public destructor
static Singleton* instance; // declaration class variable
int value;
};
Singleton* Singleton::instance = nullptr; // definition class variable
int main() {
Singleton::get().setValue(42);
std::cout << "value=" << Singleton::get().getValue() << '\n';
Singleton::destruct();
}
The program output is
value=42
This is an implementation of the Meyers singleton[5] in C++11. The Meyers singleton has no destruct method. The program output is the same as above.
#include <iostream>
class Singleton {
public:
static Singleton& get() {
static Singleton instance;
return instance;
}
int getValue() {
return value;
}
void setValue(int value_) {
value = value_;
}
private:
Singleton() = default;
~Singleton() = default;
int value;
};
int main() {
Singleton::get().setValue(42);
std::cout << "value=" << Singleton::get().getValue() << '\n';
}
Lazy initialization
A singleton implementation may use
public class Singleton {
private static volatile Singleton instance = null;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
Criticism
Some consider the singleton to be an
Singletons also violate the single-responsibility principle because they are responsible for enforcing their own uniqueness along with performing their normal functions.[8]
See also
References
- ^ ISBN 0-201-63361-2.)
{{cite book}}
: CS1 maint: multiple names: authors list (link - ^ "The Singleton design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-16.
- ^ Soni, Devin (31 July 2019). "What Is a Singleton?". BetterProgramming. Retrieved 28 August 2021.
- ^ Rainsberger, J.B. (1 July 2001). "Use your singletons wisely". IBM. Archived from the original on 24 February 2021. Retrieved 28 August 2021.
- ISBN 0-201-63371-X.
- ISBN 978-0-596-00712-6.)
{{cite book}}
: CS1 maint: multiple names: authors list (link - ^ "Why Singletons Are Controversial". Google Code Archive. Archived from the original on 6 May 2021. Retrieved 28 August 2021.
- ^ Steve Yegge. Singletons considered stupid, September 2004
- ^ Hevery, Miško, "Global State and Singletons", Clean Code Talks, 21 November 2008.
External links
![]() | This article's use of external links may not follow Wikipedia's policies or guidelines. (November 2016) |
![](http://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/40px-Wikibooks-logo-en-noslogan.svg.png)
![](http://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/30px-Commons-logo.svg.png)
- Complete article "Java Singleton Pattern Explained"
- Four 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
- Geary, David (April 25, 2003). "How to navigate the deceptively simple Singleton pattern". Java Design Patterns. JavaWorld. Retrieved 2020-07-21.
- Google Singleton Detector (analyzes Java bytecode to detect singletons)