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?

  • 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?

  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:


    To make it possible we will make the constructor private.

  2. Create a member inside the ClsSingleton class of type ClsSingleton.

    private static ClsSingleton objPriSingleton=new ClsSingleton();
  3. Create a static method of the ClsSingleton class, GetObject, which will return the objPriSingleton.
  4. public static ClsSingleton GetObject()


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:

public class ClsSingleton
          private ClsSingleton()
          public static ClsSingleton GetObject()
                   if(HttpContext.Current.Items["SingletonObject"] == null)
                             HttpContext.Current.Items["SingletonObject"] = new ClsSingleton();
HttpContext.Current.Items["SingletonObject"] as ClsSingleton;

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.

Up Next
    Ebook Download
    View all
    View all