<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Singleton_pattern</id>
	<title>Singleton pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Singleton_pattern"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Singleton_pattern&amp;action=history"/>
	<updated>2026-06-20T03:40:58Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Singleton_pattern&amp;diff=43469&amp;oldid=prev</id>
		<title>imported&gt;TarnishedPath: Per page move: Changed link from Class (computer programming) to Class (programming) (×2) using MovePlus</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Singleton_pattern&amp;diff=43469&amp;oldid=prev"/>
		<updated>2025-10-17T08:39:02Z</updated>

		<summary type="html">&lt;p&gt;Per page move: Changed link from &lt;a href=&quot;/index.php/Class_(computer_programming)&quot; class=&quot;mw-redirect&quot; title=&quot;Class (computer programming)&quot;&gt;Class (computer programming)&lt;/a&gt; to &lt;a href=&quot;/index.php/Class_(programming)&quot; title=&quot;Class (programming)&quot;&gt;Class (programming)&lt;/a&gt; (×2) using &lt;a href=&quot;/index.php?title=User:TarnishedPath/MovePlus.js&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User:TarnishedPath/MovePlus.js (page does not exist)&quot;&gt;MovePlus&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Design pattern in object-oriented software development}}&lt;br /&gt;
[[File:Singleton UML class diagram.svg|thumb|A [[class diagram]] exemplifying the singleton pattern.]]&lt;br /&gt;
&lt;br /&gt;
In [[object-oriented programming]], the &amp;#039;&amp;#039;&amp;#039;singleton pattern&amp;#039;&amp;#039;&amp;#039; is a [[software design pattern]] that restricts the [[Instantiation (computer science)|instantiation]] of a [[Class (programming)|class]] to a singular instance. It is one of the well-known [[Design Patterns|&amp;quot;Gang of Four&amp;quot; design patterns]], which describe how to solve recurring problems in object-oriented software.&amp;lt;ref name=&amp;quot;GoF&amp;quot;&amp;gt;{{cite book |author=Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides |url=https://archive.org/details/designpatternsel00gamm/page/127 |title=Design Patterns: Elements of Reusable Object-Oriented Software |publisher=Addison Wesley |year=1994 |isbn=0-201-63361-2 |pages=[https://archive.org/details/designpatternsel00gamm/page/127 127ff] |url-access=registration}}&amp;lt;/ref&amp;gt; The pattern is useful when exactly one object is needed to coordinate actions across a system.&lt;br /&gt;
&lt;br /&gt;
More specifically, the singleton pattern allows classes to:&amp;lt;ref&amp;gt;{{cite web |title=The Singleton design pattern - Problem, Solution, and Applicability |url=http://w3sdesign.com/?gr=c05&amp;amp;ugr=proble |access-date=2017-08-16 |website=w3sDesign.com}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* Ensure they only have one instance&lt;br /&gt;
* Provide easy access to that instance&lt;br /&gt;
* Control their instantiation (for example, hiding the [[Constructor (object-oriented programming)|constructors]] of a [[Class (programming)|class]])&lt;br /&gt;
&lt;br /&gt;
The term comes from the [[Singleton (mathematics)|mathematical concept of a singleton]].&lt;br /&gt;
&lt;br /&gt;
== Common uses ==&lt;br /&gt;
Singletons are often preferred to [[global variables]] because they do not pollute the global [[namespace]] (or their containing namespace). Additionally, they permit [[Lazy evaluation|lazy]] allocation and initialization, whereas global variables in many languages will always consume resources.&amp;lt;ref name=&amp;quot;GoF&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;devin&amp;quot;&amp;gt;{{cite news |last1=Soni |first1=Devin |date=31 July 2019 |title=What Is a Singleton? |work=BetterProgramming |url=https://betterprogramming.pub/what-is-a-singleton-2dc38ca08e92 |access-date=28 August 2021}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The singleton pattern can also be used as a basis for other design patterns, such as the [[Abstract factory pattern|abstract factory]], [[Factory method pattern|factory method]], [[Builder pattern|builder]] and [[Prototype pattern|prototype]] patterns. [[Facade pattern|Facade]] objects are also often singletons because only one facade object is required.&lt;br /&gt;
&lt;br /&gt;
[[Log file|Logging]] is a common real-world use case for singletons, because all objects that wish to log messages require a uniform point of access and conceptually write to a single source.&amp;lt;ref name=&amp;quot;rainsberger&amp;quot;&amp;gt;{{cite news |last1=Rainsberger |first1=J.B. |title=Use your singletons wisely |url=https://www.ibm.com/developerworks/library/co-single/ |access-date=28 August 2021 |publisher=IBM |date=1 July 2001 |archive-url=https://web.archive.org/web/20210224180356/https://www.ibm.com/developerworks/library/co-single/ |archive-date=24 February 2021}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementations ==&lt;br /&gt;
Implementations of the singleton pattern ensure that only one instance of the singleton class ever exists and typically provide [[Global scope|global access]] to that instance.&lt;br /&gt;
&lt;br /&gt;
Typically, this is accomplished by:&lt;br /&gt;
* Declaring all [[Constructor (object-oriented programming)|constructors]] of the class to be [[Private member|private]], which prevents it from being instantiated by other objects&lt;br /&gt;
* Providing a [[static method]] that returns a [[Reference (computer science)|reference]] to the instance&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This [[C++23]] implementation is based on the pre-C++98 implementation in the book {{Citation needed|reason=the book is ambiguous here.|date=March 2024}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
class Singleton {&lt;br /&gt;
private:&lt;br /&gt;
    Singleton() = default; // no public constructor&lt;br /&gt;
    ~Singleton() = default; // no public destructor&lt;br /&gt;
    inline static Singleton* instance = nullptr; // declaration class variable&lt;br /&gt;
    int value;&lt;br /&gt;
public:&lt;br /&gt;
    // defines a class operation that lets clients access its unique instance.&lt;br /&gt;
    static Singleton&amp;amp; getInstance() {&lt;br /&gt;
        if (!instance) {&lt;br /&gt;
            instance = new Singleton();&lt;br /&gt;
        }&lt;br /&gt;
        return *instance;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    Singleton(const Singleton&amp;amp;) = delete(&amp;quot;Copy construction disabled&amp;quot;);&lt;br /&gt;
    Singleton&amp;amp; operator=(const Singleton&amp;amp;) = delete(&amp;quot;Copy assignment disabled&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    static void destroy() {&lt;br /&gt;
        delete instance;&lt;br /&gt;
        instance = nullptr;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // existing interface goes here&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    int getValue() const noexcept {&lt;br /&gt;
        return value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void setValue(int newValue) noexcept {&lt;br /&gt;
        value = newValue;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    Singleton::getInstance().setValue(42);&lt;br /&gt;
    std::println(&amp;quot;value = {}&amp;quot;, Singleton::getInstance().getValue());&lt;br /&gt;
    Singleton::destroy();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The program output is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
value=42&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is an implementation of the Meyers singleton&amp;lt;ref&amp;gt;{{cite book |author=Scott Meyers |title=More Effective C++ |publisher=Addison Wesley |year=1997 |isbn=0-201-63371-X |pages= 146 ff}}&amp;lt;/ref&amp;gt; in C++11. The Meyers singleton has no destruct method. The program output is the same as above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Cpp&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
class Singleton {&lt;br /&gt;
private:&lt;br /&gt;
    Singleton() = default;&lt;br /&gt;
    ~Singleton() = default;&lt;br /&gt;
    int value;&lt;br /&gt;
public:&lt;br /&gt;
    static Singleton&amp;amp; getInstance() {&lt;br /&gt;
        static Singleton instance;&lt;br /&gt;
        return instance;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    int getValue() const noexcept {&lt;br /&gt;
        return value;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    void setValue(int newValue) noexcept {&lt;br /&gt;
        value = newValue;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    Singleton::getInstance().setValue(42);&lt;br /&gt;
    std::println(&amp;quot;value = {}&amp;quot;, Singleton::getInstance().getValue());&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Lazy initialization ===&lt;br /&gt;
&lt;br /&gt;
A singleton implementation may use [[lazy initialization]] in which the instance is created when the static method is first invoked. In [[Multithreading (software)|multithreaded]] programs, this can cause [[Race condition|race conditions]] that result in the creation of multiple instances. The following [[Java_version_history#Java_5|Java 5+]] example&amp;lt;ref&amp;gt;{{Cite book|author=Eric Freeman, Elisabeth Freeman, Kathy Sierra, and Bert Bates|title=Head First Design Patterns|publisher=O&amp;#039;Reilly Media, Inc|date=October 2004|edition=First|chapter=5: One of a Kind Objects: The Singleton Pattern|page=182|isbn=978-0-596-00712-6|chapter-url=https://books.google.com/books?id=GGpXN9SMELMC&amp;amp;pg=PA182}}&amp;lt;/ref&amp;gt; is a [[Thread safety|thread-safe]] implementation, using lazy initialization with [[double-checked locking]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Singleton {&lt;br /&gt;
    private static volatile Singleton instance = null;&lt;br /&gt;
&lt;br /&gt;
    private Singleton() {}&lt;br /&gt;
&lt;br /&gt;
    public static Singleton getInstance() {&lt;br /&gt;
        if (instance == null) {&lt;br /&gt;
            synchronized (Singleton.class) {&lt;br /&gt;
                if (instance == null) {&lt;br /&gt;
                    instance = new Singleton();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return instance;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Criticism ==&lt;br /&gt;
Some consider the singleton to be an [[anti-pattern]] that introduces [[global variables|global state]] into an application, often unnecessarily. This introduces a potential dependency on the singleton by other objects, requiring analysis of implementation details to determine whether a dependency actually exists.&amp;lt;ref name=&amp;quot;google&amp;quot;&amp;gt;{{cite web |title=Why Singletons Are Controversial |url=https://code.google.com/archive/p/google-singleton-detector/wikis/WhySingletonsAreControversial.wiki |archive-url=https://web.archive.org/web/20210506162753/https://code.google.com/archive/p/google-singleton-detector/wikis/WhySingletonsAreControversial.wiki |archive-date=6 May 2021 |access-date=28 August 2021 |website=Google Code Archive}}&amp;lt;/ref&amp;gt; This increased [[Coupling (computer programming)|coupling]] can introduce difficulties with [[unit testing]].&amp;lt;ref name=&amp;quot;button&amp;quot;&amp;gt;{{cite news |last1=Button |first1=Brian |date=25 May 2004 |title=Why Singletons are Evil |work=Being Scott Densmore |publisher=Microsoft |url=https://docs.microsoft.com/en-us/archive/blogs/scottdensmore/why-singletons-are-evil |access-date=28 August 2021 |archive-url=https://web.archive.org/web/20210715184717/https://docs.microsoft.com/en-us/archive/blogs/scottdensmore/why-singletons-are-evil |archive-date=15 July 2021}}&amp;lt;/ref&amp;gt; In turn, this places restrictions on any abstraction that uses the singleton, such as preventing [[Concurrency_(computer_science)|concurrent]] use of multiple instances.&amp;lt;ref name=&amp;quot;button&amp;quot; /&amp;gt;&amp;lt;ref&amp;gt;Steve Yegge. [http://steve.yegge.googlepages.com/singleton-considered-stupid Singletons considered stupid], September 2004&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;googletesting.blogspot.com&amp;quot;&amp;gt;Hevery, Miško, &amp;quot;[http://googletesting.blogspot.com/2008/11/clean-code-talks-global-state-and.html Global State and Singletons]&amp;quot;, &amp;#039;&amp;#039;Clean Code Talks&amp;#039;&amp;#039;, 21 November 2008.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Singletons also violate the [[single-responsibility principle]] because they are responsible for enforcing their own uniqueness along with performing their normal functions.&amp;lt;ref name=&amp;quot;button&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Initialization-on-demand holder idiom]]&lt;br /&gt;
* [[Multiton pattern]]&lt;br /&gt;
* [[Software design pattern]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
{{wikibooks|Computer Science/Design Patterns|Singleton|Singleton implementations in various languages}}&lt;br /&gt;
{{commons category}}&lt;br /&gt;
* Complete article &amp;quot;[https://howtodoinjava.com/design-patterns/creational/singleton-design-pattern-in-java/ Java Singleton Pattern Explained]&amp;quot; &lt;br /&gt;
* Four different ways to implement singleton in Java &amp;quot;[https://web.archive.org/web/20150709155148/http://www.javaexperience.com/design-patterns-singleton-design-pattern/ Ways to implement singleton in Java]&amp;quot; &lt;br /&gt;
&lt;br /&gt;
{{Design Patterns Patterns}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Singleton Pattern}}&lt;br /&gt;
[[Category:Software design patterns]]&lt;br /&gt;
[[Category:Anti-patterns]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;/div&gt;</summary>
		<author><name>imported&gt;TarnishedPath</name></author>
	</entry>
</feed>