Singleton Vs Static Classes

1:sigleton classes support Interface inheritance whereas a static class cannot implement an interface:

2:

  1. public class Singleton  
  2. {  
  3.    private static Singleton instance;  
  4.    
  5.    private Singleton() {}  
  6.    
  7.    public static Singleton Instance  
  8.    {  
  9.       get  
  10.       {  
  11.          if (instance == null)  
  12.          {  
  13.             instance = new Singleton();  
  14.          }  
  15.          return instance;  
  16.       }  
  17.    }  
  18. }
  19.    
  20.     static public class StaticSampleClass  
  21.     {  
  22.         private static readonly int SomeVariable;  
  23.         //Static constructor is executed only once when the type is first used.   
  24.         //All classes can have static constructors, not just static classes.  
  25.         static StaticSampleClass()  
  26.         {  
  27.             SomeVariable = 1;  
  28.             //Do the required things  
  29.         }  
  30.         public static string ShowValue()  
  31.         {  
  32.             return string.Format(“The value of someVariable is {0}”, SomeVariable);  
  33.         }  
  34.         public static string Message { getset; }  
  35.     }  

When shall we use singleton class and when to go for static classes?

Static classes are basically used when you want to store a single instance, data which should be accessed globally throughout your application. The class will be initialized at any time but mostly it is initialized lazily. Lazy initialization means it is initialized at the last possible moment of time. There is a disadvantage of using static classes. You never can change how it behaves after the class is decorated with the static keyword.

Singleton Class instance can be passed as a parameter to another method whereas static class cannot

Singleton Class instance can be passed as a parameter to another method whereas static class cannot

Static Initialization

  1. public sealed class Singleton  
  2. {  
  3.    private static readonly Singleton instance = new Singleton();  
  4.     
  5.    private Singleton(){}  
  6.    
  7.    public static Singleton Instance  
  8.    {  
  9.       get  
  10.       {  
  11.          return instance;  
  12.       }  
  13.    }  
  14. }  

Multithreaded Singleton

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

Resulting Context

Implementing Singleton in C# results in the following benefits and liabilities:

Benefits

  • The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.
  • The Double-Check Locking idiom described earlier in “Multithreaded Singleton” is implemented correctly in the Common Language Runtime.

Liabilities

If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.

Published by codeblogforfun

Coder, blogger, traveler

Leave a comment

Design a site like this with WordPress.com
Get started