Singleton Pattern

Singleton comes under creation type design pattern. It restricts creation of object of a class to one object.

In this pattern, a single class is responsible for creating object. It makes sure only one object is getting created, by providing way to access that object directly without creating another object throughout the application.

Static Implementation

In this implementation, we create static property which returns instance of class and declare constructor as private so that it can be instantiated by another class.

Example 

  1. public sealed class Maths {  
  2.     private Maths() {}  
  3.     private static Maths objMaths = null;  
  4.     public static Maths Instance {  
  5.         get {  
  6.             if (objMaths == null) {  
  7.                 objMaths = new Maths();  
  8.             }  
  9.             return instance;  
  10.         }  
  11.     }  
  12.     public double ValueOne {  
  13.         get;  
  14.         set;  
  15.     }  
  16.     public double ValueTwo {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public double Add() {  
  21.         return ValueOne + ValueTwo;  
  22.     }  
  23.     public double Subtraction() {  
  24.         return ValueOne - ValueTwo;  
  25.     }  
  26.     public double Multiplication() {  
  27.         return ValueOne * ValueTwo;  
  28.     }  
  29.     public double Division() {  
  30.         return ValueOne / ValueTwo;  
  31.     }  
  32. }  
  33. }   

Now, in the above example, we have created class "Maths" which has private constructor so that it can be instantiated by another class. And, it is having a static property which returns object of Maths class.

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Maths.Instance.ValueOne = 20.5;  
  4.         Maths.Instance.ValueTwo = 10.5;  
  5.         Console.WriteLine("Addition : " + Maths.Instance.Addition());  
  6.         Console.WriteLine("Subtraction : " + Maths.Instance.Subtraction());  
  7.         Console.WriteLine("Multiplication : " + Maths.Instance.Multiplication());  
  8.         Console.WriteLine("Division : " + Maths.Instance.Division());  
  9.         Console.WriteLine("\n----------------------\n");  
  10.         Maths.Instance.ValueTwo = 10.5;  
  11.         Console.WriteLine("Addition : " + Maths.Instance.Addition());  
  12.         Console.WriteLine("Subtraction : " + Maths.Instance.Subtraction());  
  13.         Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());  
  14.         Console.WriteLine("Division : " + Calculate.Instance.Division());  
  15.         Console.ReadLine();  
  16.     }  
  17. }  

Now in this Program class, we are accessing members of Maths class by static property Instance, without creating object of the class. Since we have declared constructor private, we cannot create instance of the class.

Thread-safe Implementation

In this type of implementation, thread takes out a lock on a shared object, and then checks whether or not the instance has been created before creating the instance. This takes care of the memory barrier issue. It also ensures that only one thread will create an instance.

  1. public sealed class Maths {  
  2.     private Maths() {}  
  3.     private static Maths objMaths = null;  
  4.     private static readonly object padlock = new object();  
  5.     public static Maths Instance {  
  6.         get {  
  7.             lock(padlock) {  
  8.                 if objMaths = == null) {  
  9.                 objMaths = = new Maths();  
  10.             }  
  11.             return objMaths =  
  12.         }  
  13.     }  
  14. }  
  15. public double ValueOne {  
  16.     get;  
  17.     set;  
  18. }  
  19. public double ValueTwo {  
  20.     get;  
  21.     set;  
  22. }  
  23. public double Addition() {  
  24.     return ValueOne + ValueTwo;  
  25. }  
  26. public double Subtraction() {  
  27.     return ValueOne - ValueTwo;  
  28. }  
  29. public double Multiplication() {  
  30.     return ValueOne * ValueTwo;  
  31. }  
  32. public double Division() {  
  33.     return ValueOne / ValueTwo;  
  34. }  

Next Recommended Reading
Decorator Design Pattern in C#