Singleton Design Pattern Vs Static Class

Most of the developers get confused between both. They do not understand when and why we use both of them.

Singleton

Singleton Design pattern is a part of creation design pattern. It describes that you can create only one instance of a class that will exist throughout the lifetime of an application. I mean to say if you use Singleton design pattern you cannot create multiple instance of the class.

  1. public sealed class Singleton  
  2. {  
  3.     private static volatile Singleton instance;  
  4.     private static object sync = new Object();  
  5.     private Singleton()  
  6.     {}  
  7.     public static Singleton GetInstance  
  8.     {  
  9.         get  
  10.         {  
  11.             if (instance == null)  
  12.             {  
  13.                 lock(sync)  
  14.                 {  
  15.                     if (instance == null) instance = new Singleton();  
  16.                 }  
  17.             }  
  18.             return instance;  
  19.         }  
  20.     }  
  21. }  
When to use Singleton

Singleton design pattern is a unique design pattern; you cannot find the alternative of Singleton. You can use this when you are creating or using this concept in the application like thread pools, Registry objects, SQL Connection Pools, Objects handling user preferences, Builder classes and Statistics, log4net, etc.

There is one more reason to use Singleton Design Pattern, if you think that your object is large or heavy and takes up a reasonable amount of memory and you make sure that it’s not going to be instantiated multiple times then in that scenario you can use Singleton Design Pattern. A Singleton will help prevent such a case to ever happen.

Singleton

Static class

Many developers are confused to use static class or singleton design pattern. You need to use static class when you think you don’t need to maintain the state, otherwise use singleton.

Static class contains only static members, you cannot inherit or create object of static class. A Singleton class can be inherited and also have base class. 
  1. using System;  
  2. public static class Square  
  3. {  
  4.     public static double side;  
  5.     public static double Perimeter()  
  6.     {  
  7.         return side * 4;  
  8.     }  
  9.     public static double Area()  
  10.     {  
  11.         return side * side;  
  12.     }  
  13. }  
  14. public class Exercise  
  15. {  
  16.     public static int Main()  
  17.     {  
  18.         Square.Side = 36.84;  
  19.         Console.WriteLine("Square Characteristics");  
  20.         Console.Write("Side: ");  
  21.         Console.WriteLine(Square.side);  
  22.         Console.Write("Perimeter: ");  
  23.         Console.WriteLine(Square.Perimeter());  
  24.         Console.Write("Area: ");  
  25.         Console.WriteLine(Square.Area());  
  26.         return 0;  
  27.     }  
  28. }  
Thanks for reading the article. I hope you enjoyed it. 


Recommended Free Ebook
Similar Articles