You are currently viewing What is a Singleton design pattern?

Design patterns are a vital part of software design and architecture. In this articel, we will see what are design pattern and how they are categorized. I will also explain a creational type pattern Singleton design pattern in this article.

Below are some of the important points about design patterns.

  • Design patterns are evolved as reusable solutions to the problems that we encountered every day of programming. 
  • Design patterns act as a template that we can be applied to real-world problems.
  • Writing the code aligning with design pattern will make applications reliable, scalable and easily maintainable.

Categorization of design patterns

The four authors of the book “element of reusable object-oriented software” are referred to as Gang of Four and then, Gang of Four design patterns have made a significant role in the software development life cycle. They have categorized the design patterns based on different problems encountered in real-time applications.

  1. Creational: This type deals with object creation and initialization. This pattern gives the program more flexibility in deciding which objects need to create for a given case.
    e.g. Singleton, Factory, Abstract Factory, etc.
  2. Structural: This type deals with class and object composition. In a simple word, this pattern focuses on decoupling interface and implementation of classes and its objects.
    e.g. Adapter, bridge, etc.
  3. Behavioral: This type deals with communications between classes and objects.
    e.g. Chain of Responsibility, Command, and Interpreter, etc.

Singleton Design Pattern

Singleton design patterns belong to creational type pattern. This pattern is used when we need to ensure that only one object of a particular class needs to be created. All further references to the objects are referred to as the same underlying instance created.

Advantages of Singleton

  • Singleton controls concurrent access to the resources.
  • It ensures there is only one object available across the application in a controlled state.

How to Implement Singleton

  1. Ensure there is only one instance exists of the singleton class.
  2. Provide Global access to the instance by
    1. Declaring all the constructors of the class to be private.
    2. Declare the singleton class with the sealed keyword.
    3. providing a static method that returns a reference to the instance.
    4. The instance is stored as a private static variable.

In Multithreaded application violence of creating an instance of the singleton class, it means it has created multiple instances of the singleton class, so to avoid this we can use the double-checking locking mechanism.

Example:

private static Employee instance = null;
private static readonly object obj = null;
 
public static Employee GetInstance
{
    get 
      {
          if (instance == null)
          {
              lock(obj)
              {
                if (instance == null)    
                    instance = new Employee();           
              }
          }
          return instance;
      }
}

This is efficient because it avoids a potentially expensive lock operation if a value has already been assigned to the instance. However, this code is incorrect because the field instance isn’t volatile, which could result in instance being computed twice on some systems.

To overcome this problem, we will use lazy loading or Eager loading of the object. Eager

Loading/Non-Lazy Loading

  • Pre-Instantiation of the object (initialize the object before it access)
  • Commonly used in low memory footprints. e.g. Eager loading Example
private static readonly Employee instance = new Employee();
 
public static Employee GetInstance
{
    get 
      {      
          return instance;
      }
}

Eager loading created a single instance of employee object in multithreading calls of GetInstance property with thread safety, and CLR will take care of this.

Lazy Loading

private static readonly Lazy<Employee> instance = new Lazy<Employee>(
()=> Employee());
 
public static Employee GetInstance
{
    get 
      {      
          return instance.Value;
      }
}

We have created an object with Lazy keyword, by default Lazy objects are thread-safe so in the multithreaded scenario, we can also use lazy loading. 

Advantage Of using lazy Loading 

  • Improve the performance 
  • Avoids Unnecessary computation until the point object is accessed.
  • Reduces the memory footprint on the start-up.
  • Faster application load.

Difference between Singleton and Static Class.

Singleton Static
Singleton is a design pattern. Static is a keyword.
Singleton is an object creation pattern with one instance of the class. Static classes just provide static methods
Singleton can Implement Interfaces, inherit from other classes. Static class can’t Implement Interfaces, and can’t inherit from other classes.
Singleton object can be passed as reference. Static class can’t be passed as reference.
Singleton supports object disposal. Static objects dispose of manually.
Singleton objects can be cloned. Static class can’t be cloned

That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our facebook page, follows us on twitter and subscribe to our YouTube channel for latest updates.

Leave a Reply