All about Singleton Pattern

What is singleton Pattern?

Singleton pattern is the design pattern that restrictions the number of instances of the class to one any time.

That is any time only one instance of the class will be existed and this instance has global point of access. This pattern will be helpful when we need exactly one object is required not more or less.

How to implement?

If we want to implement singleton pattern we need to consider following:

  1. If we want to restrict object creating (instantiation) we should make the constructor of the class to private.

  2. If we want to create object of class having private constructor then we should have one Method in the class to create object and returns the object, let us call this method as Object Creator.

  3. We should able to call the Object Creator of the class with out object of the class (Why because we are calling this creator to create object so we don’t have any object at this moment). So that Object Creator should be public and static so that we can call the method with out object from the outside of the class

  4. We need one singleton class variable in the class to hold the singleton object created by the creator this should not be accessed from outside so it should be private, and it should accessed from Object Creator method which is static so this variable should be static.

In short if we want to implement singleton pattern we need a class with only private constructor(s), one static private variable to hold the singleton object and with public static method which create the object and returns to the caller.

Following is the sample code for the singleton design pattern:

Code

  1. class SingletonClass  
  2. {  
  3.       /// <summary>  
  4.       /// This object stores the singleton object  
  5.       /// </summary>  
  6.       private static SingletonClass _singletonClassObject;  
  7.       //private static SingletonClass _singletonClassObject = new SingletonClass();  
  8.       private static object syncLock = new object();    
  9.   
  10.       /// <summary>  
  11.       /// This constructor should be private so that we can restrict users to create the object of this class  
  12.       /// </summary>  
  13.       private SingletonClass()  
  14.       {  
  15.             //Constructor login  
  16.       }    
  17.   
  18.       /// <summary>  
  19.       /// This is Object creator of the singleton class. We need to call this outside of the class with out object of this class so this should be public & static  
  20.       /// </summary>  
  21.       /// <returns></returns>  
  22.       public static SingletonClass GetSingletonClassObject()  
  23.       {  
  24.            lock (syncLock) // this is for synchronize lock to confirm no to code paths calls this method object creation method at a time.
                {
  25.                if (_singletonClassObject == null)  
  26.                {  
  27.                   _singletonClassObject = new SingletonClass();  
  28.                }  
  29.             }  
  30.             return _singletonClassObject;  
  31.       }  
  32.   
  33.       public void privateMethod()  
  34.       {  
  35.             //Private method logic here  
  36.       }  
  37. }  
What are the best practices? 
  • Better to use the lazy lazy initialization

    What is lazy initialization?? above way of checking for the _singletonClassObject is null or not and creating object is called lazy initilization instead of creating object like this we can create object like the below also, this is called eager initialization
    1. private static SingletonClass _singletonClassObject = new SingletonClass();  
    2. public static SingletonClass GetSingletonClassObject() {return _singletonClassObject; }  
Why we need lazy initialization?

Usually we will create singleton object for holding costly/heavily resources so better to create object when ever we required instead of creating object initial block(while initialization).
  • Better to use synchronization while creating singleton object. Better to use above object creator instead of following
    1. public static SingletonClass GetSingletonClassObject()  
    2. {  
    3.       if (_singletonClassObject == null) { _singletonClassObject = new SingletonClass(); } return _singletonClassObject;  
    4. }  
But this method should be avoid. Why? Singleton pattern means creating object of the class only once. If we didn’t use the synchronization mechanism then there might be chance to create more than one object if more than one code path calls the Object creator same time. so better to use lock for the object creation block. What is the Use? There are some situations which we can use only one instance of the Class will be required more than one instances may not be allowed like:
  • Net work resources manager.
  • File management system.
  • Error longing in the application.
  • Database access.
For the situations like theses where we can create only instance of the class we can user singleton pattern.

Hope this post will be helpful for understanding singleton pattern.