Wednesday, February 20, 2008

Singleton Design pattern

This is my first blog in a series of blog which I am planning to write on design patterns. The reason why I am planning to write blog on design pattern is that in the interviews I conduct 99% people are not aware of design patterns. To start with I will blog on singleton design pattern. Singleton is one of the simplest design pattern and it is very easy to implement. It falls under the creational pattern of the Gang Of Four’s (GOF) design pattern. First lets see what is singleton design pattern.


What is singleton design pattern?
Singleton design pattern is one which says that at any point there should be only one instance of the class present through out the lifecycle of the application. In other words the singleton class should take care that there should not be more than one instance of the class in memory till the application is alive. So how can I achieve this? To know read on.....

Pattern were developed to solve a set of problem and the problem singleton solves is there should exists only one instance of a class and the principle to solve the problem are as follows.

1. Declare the class constructor private
2. Once constructor is declared private then one cannot create the instance of the class, so how to create an instance? Expose a static method which creates an instance of the same class.
3. Have a class level static variable which is of the same type as that of the singleton class itself.

Now we have the principles behind the singleton, lets see how we can achieve this programmatically.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
#region Singleton
public class Singleton
{
///


/// The static variable of the class which will hold the instance of the class.
///

private static Singleton singleInstance;
private static int counter;
///
/// Private constructor which will restrict the direct instance creation.
///

private Singleton()
{
counter++;
}
///
/// The static method which will take care of creation of only instance of class by checking
/// the static variable for not null.
///

///
public static Singleton CreateInstance()
{
if (singleInstance == null)
{
singleInstance = new Singleton();
}
return singleInstance;
}

public void PrintInstanceCount()
{
Console.WriteLine(counter);
Console.ReadLine();
}
}
#endregion

class Program
{
static void Main(string[] args)
{
Singleton s1 = Singleton.CreateInstance();
Singleton s2 = Singleton.CreateInstance();
Singleton s3 = Singleton.CreateInstance();
Singleton s4 = Singleton.CreateInstance();
Singleton s5 = Singleton.CreateInstance();
Singleton s6 = Singleton.CreateInstance();
s1.PrintInstanceCount();
s2.PrintInstanceCount();
s3.PrintInstanceCount();
s4.PrintInstanceCount();
s5.PrintInstanceCount();
s6.PrintInstanceCount();
}
}
}

From the above code one can infer that we have a class called “Singleton” which implements singleton design pattern. It has got a private constructor which will prevent the direct creation of the class. Then we have static variable called “singleInstance” which will hold the instance of the “Singleton” class. And finally we have a static method called “CreateInstance” which takes care of creating the class. So at any given point of time if you want to create an instance of the class you do it by calling the static method "CreateInstance". Inside the method we have a "if" checking which checks whether the static variable (singleInstance) is null, if it is, then the method creates a new instance of the class assigns the new object to the "singleInstance" static variable and returns it else it returns the same object which is already created and assigned in the static variable, singleInstance.

So from above explanation it would have been clear on how to implement singleton design pattern. The method "CreateInstance" in conjunction with the private constructor and static variable makes sure that at any given point there exits only one instance of the class in memory.