Implementing Singleton Design Patterns

Singleton Pattern

The Singleton Pattern is a type of Creational Pattern of the Gang of Four Design Patterns. Before moving to the Singleton Design Pattern, let's first understand what a singleton is. A singleton is a class that allows only one object to be created. Hence, the Singleton Design Pattern is a pattern that restricts the instantiation of the class to one object only and provides a global point of access to it.

Characteristics of the Singleton Design Pattern

  •       They have a single constructor that is private and parameterless. This helps in two ways. First, this restricts the class to instantiation of only one object. Second, this class cannot be inherited (if the class can be inherited tehn all the derived classes could create their instances and that would violate this pattern).
  •       This class is sealed. This basically helps the JIT to optimize the things.
  •       This class contains a static variable that holds the reference of the object created.
  •       This class contains a public static, in other words a way of getting the reference of the object created.

Implementation of Singleton Design Pattern

The following is the code for a thread-safe implementation of the Singleton Pattern:

  1. //sealed class  
  2. public sealed class Singleton  
  3. {  
  4.     //private, parameterless constructor  
  5.     private Singleton()  
  6.     {  
  8.     }  
  9.     //static variable that will hold the reference of the object created  
  10.     public static Singleton instance = null;   
  12.     //an object to make the implementation thread safe  
  13.     public static readonly object _lock = new object();          
  15.     //pubic static means of getting the reference of the object created  
  16.     public static Singleton GetInstance  
  17.     {  
  18.         get  
  19.         {  
  20.             //to make implementation thread-safe  
  21.             lock (_lock)  
  22.             {  
  23.                 if (instance == null)  
  24.                 {  
  25.                     instance = new Singleton();  
  26.                 }  
  27.                 return instance;  
  28.             }  
  29.         }  
  30.     }  

In the code, the Singleton Design Pattern is thread-safe. Here the thread takes the lock on the shared object and checks whether or not the instance existed earlier or not, if the instance is null then only then is the instance created. This way it ensures that only one thread can create the instance.

Now in the main method we can fetch the instance of the singleton class using the public static reference as in the following:

  1. Singleton obj = Singleton.GetInstance; 

Using this object we can make calls to any of the methods of the singleton class.

The following are the differences between a static class and a singleton class:
  • A singleton class can create only one instance of a specific class. That instance can be passed as a parameter to a method or used to call the singleton class members. Whereas a static class can only have static members and a static class cannot be passed as a parameter.
  • Implementation inheritance is possible with a singleton class, but not with a static class.
  • A singleton class can be initialized in a lazy manner whereas a static class is initialized when it is loaded.
  • Singleton objects are stored on the heap whereas a static class is stored in the stack (since the scope of a static class is the application domain so it is not stored on the heap).