Wednesday, 22 February 2017

Singleton Design Pattern

Ensure a class has only one instance and provide a global point of access to it.

What is singleton pattern?
The singleton pattern is one of the simplest design patterns:
There are only two points in the definition of a singleton design pattern,
1-there should be only one instance allowed for a class and
2-we should allow global point of access to that single instance.

This structural code demonstrates the Singleton pattern which assures only a single instance
(the singleton) of the class can be created.
 using System;  
 namespace Singleton.Structural  
  /// <summary>  
  /// MainApp startup class for Structural  
  /// Singleton Design Pattern.  
  /// </summary>  
  class MainApp  
   /// <summary>  
   /// Entry point into console application.  
   /// </summary>  
   static void Main()  
    // Constructor is protected -- cannot use new  
    Singleton s1 = Singleton.Instance();  
    Singleton s2 = Singleton.Instance();  
    // Test for same instance  
    if (s1 == s2)  
     Console.WriteLine("Objects are the same instance");  
    // Wait for user  
  /// <summary>  
  /// The 'Singleton' class  
  /// </summary>  
  class Singleton  
   private static Singleton _instance;  
   // Constructor is 'protected'  
   protected Singleton()  
   public static Singleton Instance()  
    // Uses lazy initialization.  
    // Note: this is not thread safe.  
    if (_instance == null)  
     _instance = new Singleton();  
    return _instance;  

when to use Singleton classes

A very simple example is say Logger, suppose we need to implement the logger and log it to some file according to date time. In this case, we cannot have more than one instances of Logger in the application otherwise the file in which we need to log will be created with every instance.
We use Singleton pattern for this and instantiate the logger when the first request hits or when the server is started.

Difference between static class and static method approaches:One question which a lot of people have been asking me. What’s the difference between a singleton class and a static class? The answer is static class is one approach to make a class “Singleton”.
We can create a class and declare it as “final” with all the methods “static”. In this case, you can’t create any instance of class and can call the static methods directly.

Here is how it looks:

public class Singleton {
   private static final Singleton instance;   
   private Singleton(){}
   public static Singleton getInstance() {
     if (instance == null)
       instance = new Singleton();
     return instance;

As you can see, the constructor is private, so we are unable instantiate it in the normal fashion. What you have to do is call it like this:

public Singleton singleton = Singleton.getInstance();

When you do this, the getInstance() method then checks to see if the parameter ‘instance’ is null. If it is, it will create a new one by calling the private constructor. After that, it just returns it. Of course, if it is not null, it just returns the existing instance of it. This insures that there is only one copy of the object within your program.


Post a Comment


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)