Singleton Design Pattern

Introduction 
 
Why do we even need a singleton pattern? Consider a situation where you only want to create one instance of the class. For example, let’s say you’re calling an API in your project, as every API’s first step is to authenticate the client. After successful authentication, the server generates a session, which is alive for some time. Imagine for every single request you're creating a session, which is wrong at so many levels. You should only create a session once at use that session for all other requests (for example, Facebook's login to logout is one session). This is where we should use the singleton pattern. 
  1. namespace SingletonPattern  
  2. {  
  3.     /// <summary>  
  4.     /// Basic implementation of Singleton design pattern  
  5.     /// </summary>  
  6.     class SingletonExample  
  7.     {    
  8.         /// <summary>  
  9.         /// As there is single instance for entire project it has to be static.  
  10.         /// </summary>  
  11.         private static SingletonExample obj;  
  12.   
  13.         /// <summary>  
  14.         /// Default constructor  
  15.         /// </summary>  
  16.         public SingletonExample()  
  17.         {  
  18.   
  19.         }  
  20.   
  21.         /// <summary>  
  22.         /// static method which initialise our static instance  
  23.         /// </summary>  
  24.         /// <returns>SingletonExample</returns>  
  25.         public static SingletonExample Initialise()  
  26.         {  
  27.             if(obj == null)  
  28.             {  
  29.                 obj = new SingletonExample();  
  30.             }  
  31.             return obj;  
  32.         }  
  33.     }  
  34. }  
 
As you see in the above code we have static Initialise() method, so whenever your project calls this method for the first time, it creates a new object then after that it will just return the same instance.
 
There's a problem with the above code, as it's not thread-safe. It creates an instance for every thread that makes a request.
 
It's ok, no need to panic, as we have a solution for such cases. We have 2 keywords to help us out:
 
Volatile and Synchronised
 
Volatile (in brief):
 
In a multiThreaded scenario, one Thread which has a processor can cache some resource (properties/variables) out of your class in its cache memory for faster processing and where the other Thread holding the same old values resource which is on RAM. Here we have a mismatch issue, so in order to avoid that we use volatile. It makes sure cache data is updated on RAM and vice-versa.
 
Synchronised (in brief):
 
Synchronization allows only one thread to access the resource at a time. No other thread can get access to that resource until the assigned thread finishes its task. Threads execute asynchronously. Synchronization helps us by making threads synchronized.
 
We can use a locking mechanism to make it thread-safe.
 
C# Implementation (unsynchronized in C#, but we have a workaround)
  1.   /// <summary>  
  2.   /// Basic implementation of Singleton design pattern  
  3.   /// </summary>  
  4.   class SingletonExample  
  5.   {  
  6.       /// <summary>  
  7.       /// As there is single instance for entire project it has to be static.  
  8.       /// </summary>  
  9.       private volatile static SingletonExample obj = null;  
  10.   
  11.       /// <summary>  
  12.       /// static object class instance  
  13.       /// </summary>  
  14.       private static readonly object SyncLock = new object();  
  15.   
  16.       /// <summary>  
  17.       /// Default constructor  
  18.       /// </summary>  
  19.       public SingletonExample()  
  20.       {  
  21.   
  22.       }  
  23.   
  24.       /// <summary>  
  25.       /// static method which initialise SingletonExample  
  26.       /// </summary>  
  27.       /// <returns>instance of SingletonExample</returns>  
  28.       public static SingletonExample Initialise()  
  29.       {  
  30.           if(obj == null)  
  31.           {  
  32.               lock (SyncLock)  
  33.               {  
  34.                   if (obj == null)  
  35.                   {  
  36.                       obj = new SingletonExample();  
  37.                   }  
  38.               }  
  39.           }  
  40.           return obj;  
  41.       }  
  42.   }  
 In Java we have synchronized keyword to take the pain away.
  1. class SingletonExample  
  2.     {  
  3.         /// <summary>  
  4.         /// As there is single instance for entire project it has to be static.  
  5.         /// </summary>  
  6.         private volatile static SingletonExample obj;  
  7.   
  8.         /// <summary>  
  9.         /// Default constructor  
  10.         /// </summary>  
  11.         public SingletonExample()  
  12.         {}  
  13.   
  14.         /// <summary>  
  15.         /// static method which initialise our static instance  
  16.         /// </summary>  
  17.         /// <returns>SingletonExample</returns>  
  18.         public static SingletonExample Initialise()  
  19.         {  
  20.             if(obj == null)  
  21.             {  
  22.                 synchronized(SingletonExample.class)   
  23.             {   
  24.                 if (obj == null)   
  25.                     obj = new SingletonExample();  
  26.                 }  
  27.             }  
  28.             return obj;  
  29.         }  
  30.     }  
Thank you so much for visiting this blog, I hope you were helped by this. If you have any queries, please connect with me.
 
Have a good day :)