Learn Design Pattern - Singleton Pattern

In this article we will try to understand what Single Pattern is, what kind of problem it solves and how it should be implemented in ASP.NET.

In my last article we already talked about Design Pattern basics.

  • They provide solutions to common recurring problems.
  • Design patterns can be classified as creational, structural or behavioral.

What is Singleton Pattern?

  • The Singleton Design Pattern ensures that only a single instance of a given object can exist at a context.
  • As it solves problems related to object creation, it falls under creational pattern.

When to use
Singleton Pattern?

  • The Find Box in the browser or even in Visual studio (press Ctrl+F ) is the best example of a Singleton class. If you notice all the tabs share a common Find Box.
  • A class that wraps the settings related to an application can be made a singleton class.
  • For the On Screen Keyboard provided in the Windows operating system, we use the same keyboard object for every application.

In short, whenever we want something to be shared across multiple locations, we use a singleton pattern.

How to Create Singleton Pattern?

Singleton Pattern

  1. First we need to restrict the user from creating instances directly.

    In other words, we have to stop the user from doing something like this:

    ClsSingleton.jpg

    To make it possible we will make the constructor private.

    Class-Cls-singleton.jpg
     
  2. Create a member inside the ClsSingleton class of type ClsSingleton.
    1. private static ClsSingleton objPriSingleton=new ClsSingleton();
  3. Create a static method of the ClsSingleton class, GetObject, which will return the objPriSingleton.
    1. public static ClsSingleton GetObject()  
    2. {  
    3.      return objPriSingleton;  
    4. }

Note:

There are other approaches for creating a singleton class, and I just explained the one used most commonly, you can find other approaches like the static constructor approach in the source code attached.

Singleton Patterns in ASP.NET


Now let's talk about static objects, they maintain their values and reside in the memory as long as the application which contains it does.

In the singleton pattern we saw, the singleton object is marked as static.

Sharing across all users

We can use a classic approach only when we want something to be shared across all users.

Sharing across a request

In one of my previous articles about the ASP.NET life cycle we learned how an ASP.NET request works in which various modules and handlers are relevant.

When something must be shared across this request pipeline, we should replace static with HttpContext.Current.Items.

Sharing across a single user

When something must be shared across all requests related to a single user, static must be replaced with HttpContext.Current.Session.

This is how the code looks like:

  1. public class ClsSingleton  
  2. {  
  3.     private ClsSingleton()  
  4.     {                   
  5.     }  
  6.     public static ClsSingleton GetObject()  
  7.     {  
  8.         if(HttpContext.Current.Items["SingletonObject"] == null)  
  9.             HttpContext.Current.Items["SingletonObject"] = new ClsSingleton();  
  10.         return HttpContext.Current.Items["SingletonObject"as ClsSingleton;  
  11.     }  
  12. }  

 

Please find the attached ASP.NET source code to see how a Singleton pattern is implemented in ASP.NET.

Comments are always there for queries.

Hope you enjoyed learning the Singleton pattern. In the subsequent article we will explore some patterns and see how and when to implement them in C#.

Hope to see some good comments.


Similar Articles
Just Compile LLP
Just Compile is an IT based firm based out of Mumbai. Application Development, Staffing, Training