Singleton pattern: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Changed lazy-loaaded solution to Pugh's. Removed second lazy-loaded solution, it served little purpose.
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The '''singleton''' [[design pattern (computer science)|design pattern]] is designed to restrict instantiation of a class to one (or a few) [[object-oriented programming|objects]]. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an [[Anti-pattern]] since it is often used as a politically correct term for [[global variable]], and hence frowned upon. Before designing a class as a singleton, it is wise to consider whether it would be enough to design a normal class and just use one instance.
The '''singleton''' [[design pattern (computer science)|design pattern]] is designed to restrict instantiation of a class to one (or a few) [[object-oriented programming|objects]]. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an [[anti-pattern]] since it is often used as a politically correct term for [[global variable]], and hence frowned upon. Before designing a class as a singleton, it is wise to consider whether it would be enough to design a normal class and just use one instance.


The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the [[constructor]] is made either private or protected. Note the distinction between a simple static instance of a class and a singleton. Although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed.
The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the [[constructor]] is made either private or protected. Note the distinction between a simple static instance of a class and a singleton. Although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed.
Line 6: Line 6:


The classic solution to this problem is to use [[mutual exclusion]] on the class that indicates that the object is being instantiated.
The classic solution to this problem is to use [[mutual exclusion]] on the class that indicates that the object is being instantiated.

== Example implementations ==


A '''correct''' threadsafe [[Java programming language]] [[lazy initialization|lazy-loaded]] solution known as the "Initialization On Demand Holder" idiom suggested by Bill Pugh follows:
A '''correct''' threadsafe [[Java programming language]] [[lazy initialization|lazy-loaded]] solution known as the "Initialization On Demand Holder" idiom suggested by Bill Pugh follows:
Line 63: Line 65:


In a [[prototype-based programming]] language, where objects but not classes are used, a "singleton" simply refers to an object without copies or that is not used as the prototype for any other object.
In a [[prototype-based programming]] language, where objects but not classes are used, a "singleton" simply refers to an object without copies or that is not used as the prototype for any other object.

== Example of usage with the factory method pattern ==

The singleton pattern is often used in conjunction with the [[factory method pattern]] to create a system-wide resource whose specific type is not known to the code that uses it. An example of using these two patterns together is the Java [[Abstract Windowing Toolkit]] (AWT).

{{Javadoc:SE|package=java.awt|java/awt|Toolkit}} is an [[abstract class]] that [[binding (computer science)|binds]] the various AWT components to particular native toolkit implementations. The <code>Toolkit</code> class has a {{Javadoc:SE|java/awt|Toolkit|getDefaultToolkit()}} factory method that returns the [[Platform-specific model|platform-specific]] [[subclass]] of <code>Toolkit</code>. The <code>Toolkit</code> object is a singleton because the AWT needs only a single object to perform the binding and the object is relatively expensive to create. The toolkit methods must be implemented in an object and not as [[static method]]s of a class because the specific implementation is not known by the platform-independent components. The name of the specific <code>Toolkit</code> subclass used is specified by the "<tt>awt.toolkit</tt>" [[environment property]] accessed through {{Javadoc:SE|java/lang|System|getProperties()}}.

The binding performed by the toolkit allows, for example, the backing implementation of a {{Javadoc:SE|package=java.awt|java/awt|Window}} to bound to the platform-specific <code>java.awt.peer.WindowPeer</code> implementation. Neither the <code>Window</code> class nor the application using the window needs to be aware of which platform-specific subclass of the peer is used.


==See also==
==See also==
*[[Double checked locking pattern]]
*[[Double-checked locking]]
*[[Multiton pattern]]
*[[Multiton pattern]]



Revision as of 18:20, 1 February 2006

The singleton design pattern is designed to restrict instantiation of a class to one (or a few) objects. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern since it is often used as a politically correct term for global variable, and hence frowned upon. Before designing a class as a singleton, it is wise to consider whether it would be enough to design a normal class and just use one instance.

The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected. Note the distinction between a simple static instance of a class and a singleton. Although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed.

The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one.

The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated.

Example implementations

A correct threadsafe Java programming language lazy-loaded solution known as the "Initialization On Demand Holder" idiom suggested by Bill Pugh follows:

public class Singleton {
    // Private constructor suppresses generation of a (public) default constructor
    private Singleton() {}

    public static Singleton getInstance() {
        return SingletonHolder.instance;
    }

    private static class SingletonHolder {
           private static Singleton instance = new Singleton();
    }
}

An incorrect lazy-loaded example follows. It is left here to demonstrate a common error in Java. It is based on the Q&A link found below, modified for multi-threading, however, it is still vulnerable to the double-checked locking anti-pattern, also found below:

public class Singleton {
    private static Singleton INSTANCE = null;

    // Private constructor suppresses 
    private Singleton() {}

    //synchronized creator to defend against multi-threading issues
    //another if check here to avoid multiple instantiation
    private synchronized static void createInstance() {
        if (INSTANCE == null) { 
            INSTANCE = new Singleton();
        }
    }

    public static Singleton getInstance() {
        if (INSTANCE == null) createInstance();
        return INSTANCE;
    }
}

A possible C++ solution (also known as Meyers singleton) where the singleton is a static local object (note: this solution is not thread-safe and is designed to give an idea of how singletons work rather than a solution usable in large-scale software project).

template<typename T> class Singleton
{

  public:
    static T& Instance()
    {
        static T theSingleInstance; //assumes T has a default constructor
        return theSingleInstance;
    }
};
class OnlyOne : public Singleton<OnlyOne>
{
    //..rest of interface defined here
};

In a prototype-based programming language, where objects but not classes are used, a "singleton" simply refers to an object without copies or that is not used as the prototype for any other object.

Example of usage with the factory method pattern

The singleton pattern is often used in conjunction with the factory method pattern to create a system-wide resource whose specific type is not known to the code that uses it. An example of using these two patterns together is the Java Abstract Windowing Toolkit (AWT).

java.awt.Toolkit is an abstract class that binds the various AWT components to particular native toolkit implementations. The Toolkit class has a Toolkit.getDefaultToolkit() factory method that returns the platform-specific subclass of Toolkit. The Toolkit object is a singleton because the AWT needs only a single object to perform the binding and the object is relatively expensive to create. The toolkit methods must be implemented in an object and not as static methods of a class because the specific implementation is not known by the platform-independent components. The name of the specific Toolkit subclass used is specified by the "awt.toolkit" environment property accessed through System.getProperties().

The binding performed by the toolkit allows, for example, the backing implementation of a java.awt.Window to bound to the platform-specific java.awt.peer.WindowPeer implementation. Neither the Window class nor the application using the window needs to be aware of which platform-specific subclass of the peer is used.

See also

External links