Jul 7, 2013

Singleton Design Pattern

1 comment
The Singleton pattern is a pattern that makes sure that a class has only a single instance, and provides a global access point to it.
A class that implements this pattern is itself responsible for keeping track of its only instance rather than relying on global variables. The file system of Windows is a piece like this.

Let's say we are creating an application that has reporting functionality, and the client wants to maintain a count of all reports that are generated & printed. Since the Singleton design pattern allows for only a single instance and it is a global access point for the reporting module, this would allow us, the designer, to ensure that an accurate count is always maintained, without having to rely on global statics variables.


Implementation:
Let's take a look at an implementation of a class using the Singleton design pattern. This example of Singleton takes into account thread-safety by using locking. It takes out a lock on the Singleton class (to ensure no race condition happens) and checks whether an instance already exists. If no instance is found then it creates an instance, otherwise it returns the instance already available, Locking ensures that one thread can create an instance
public sealed class Singleton
{
    private static Singleton _instance = null;
    private static readonly object _threadSafetyLock = new object();

    Singleton() { }

    public static Singleton Instance
    {
        get
        {
            lock (_threadSafetyLock)
            {
                if (_instance == null)
                    _instance = new Singleton();
                return _instance;
            }
        }
    }
}
A second way of accomplishing and implementing the Singleton design pattern is to use a double-locking technique. This technique solves the concurrency issue, avoiding an exclusive lock whenever the Instance property is called. This approach also allows the developer to delay the instantiation of the object until it is first called upon. Let's take a look at the double-locking technique when implementing the Singleton design pattern:
public sealed class Singleton
{
    static volatile Singleton _instance = null;
    static readonly object _threadSafetyLock = new object();

    Singleton() { }

    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (_threadSafetyLock)
                {
                    if (_instance == null)
                        _instance = new Singleton();
                }
            }
            return _instance;
        }
    }
}
Notice we mark _instance property variable as volatile, this ensures that its value is set before it can be accessed. This ensures that the _instance always contains the most current value.
So, that is the Singleton Design Pattern.

Common uses:

  1. Abstract factory, builder and prototype patterns can use Singletons in their implementations
  2. Singletons are often preferred to global variables, because they permit lazy allocation and initialization, whereas global variables will always consume resources.

1 comment :