Jump to content

Singleton pattern

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Telemachus12389 (talk | contribs) at 18:17, 13 October 2022 (Implementations: Remove redundant code examples. Java, as the archetypal OOP language, is all that's really needed.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
A class diagram exemplifying the singleton pattern.

In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. One of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems in object-oriented software,[1] the pattern is useful when exactly one object is needed to coordinate actions across a system.

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

Logging is a classic example of a singleton.[4]

Criticism

Critics consider the singleton to be an anti-pattern as it introduces global state into an application, often unnecessarily. This in turn can place restrictions on any abstraction that uses the singleton, for example by preventing concurrent use of multiple instances.[5][6][7][8] Furthermore, because it is often exposed as a globally visible accessor, its presence complicates dependency analysis by introducing a potential dependency on the singleton in all code it is visible to, thus requiring analysis of implementation details to determine if a dependency actually exists.[9]

Singletons also violate the single-responsibility principle,[5] because not only are they responsible for the singleton's normal task, it must also ensure that only one is instantiated; note that this relies on a "classic" singleton definition where it is responsible for enforcing its own uniqueness through, for example, a static getInstance() method.

Another drawback is that singletons are difficult to test [dubiousdiscuss] because they carry global state for the duration of the program [dubiousdiscuss]. Specifically, because unit testing requires loosely coupled classes in order to isolate what's being tested.[5] Additionally, when a certain class interacts with a singleton, that class and the singleton become tightly coupled [dubiousdiscuss], making it impossible to test the class on its own without also testing the singleton [dubiousdiscuss].[9]

Implementations

Implementations of the singleton pattern ensure that only one instance of the singleton class ever exists, and typically provide global access to that instance.

Typically, this is done by:

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 snippet provides an example implementation, written in Java:[10]

public class Coin {

    private static final int ADD_MORE_COIN = 10;
    private int coin;
    private static Coin instance = new Coin(); // eagerly loads the singleton

    private Coin() {
        // private to prevent anyone else from instantiating
    }

    public static Coin getInstance() {
        return instance;
    }

    public int getCoin() {
        return coin;
    }

    public void addMoreCoin() {
        coin += ADD_MORE_COIN;
    }

    public void deductCoin() {
        coin--;
    }
}

Lazy initialization

A singleton implementation may use lazy initialization, where the instance is created when the static method is first invoked. In multi-threaded programs, this can cause race conditions that result in the creation of multiple instances. The following is a thread-safe implementation, using lazy initialization with double-checked locking, written in Java.

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;
    }
}

See also

References

  1. ^ a b Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 127ff. ISBN 0-201-63361-2.{{cite book}}: CS1 maint: multiple names: authors list (link)
  2. ^ "The Singleton design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-16.
  3. ^ Soni, Devin (31 July 2019). "What Is a Singleton?". BetterProgramming. Retrieved 28 August 2021.
  4. ^ Rainsberger, J.B. (1 July 2001). "Use your singletons wisely". IBM. Archived from the original on 24 February 2021. Retrieved 28 August 2021.
  5. ^ a b c Button, Brian (25 May 2004). "Why Singletons are Evil". Being Scott Densmore. Microsoft. Archived from the original on 15 July 2021. Retrieved 28 August 2021.
  6. ^ Steve Yegge. Singletons considered stupid, September 2004
  7. ^ Hevery, Miško, "Global State and Singletons", Clean Code Talks, 21 November 2008.
  8. ^ Contieri, Maximiliano (13 July 2020). "Singleton Pattern: The Root of All Evil". Hacker Noon. Retrieved 28 August 2021.
  9. ^ a b "Why Singletons Are Controversial". Google Code Archive. Archived from the original on 6 May 2021. Retrieved 28 August 2021.
  10. ^ "Are you an Android Developer and not using Singleton Class yet?". 16 April 2020.