Singleton Design Patterns

Before discussing singleton design pattern let me give you some basic background of design pattern and its history

The idea of design pattern was picked by four authors: Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm, very well known by the Gang of Four where they applied a concept of typical programming. They came up with 23 design pattern for solving various real-life problems of object-oriented design.

A design pattern is one kind of recipes book to deliver delicious and tasty programming dishes 😉 A Design pattern act as a template which can be applied to the real world problem

Writing a code using design pattern makes your code more reliable, scalable and maintainable

There are 3 type of design pattern

  • Creational
  • Structural
  • Behavioral

Creational: This design pattern gives you the functionality of object creation and initialization based on different cases. most popular creational design pattern are Factory, Singleton

Structural: This type of design pattern is responsible for building simple and efficient class hierarchies and a relation between different classes. for example Adapter, bridge

Behavioural: This design pattern deals with communication between classes and objects. for example Chain of Responsibility, Command, Interpreter etc

Singleton Design Pattern

Singleton design pattern is a creational design pattern which let you ensure that only single object is created for a particular class. All further references to the class are referred to this object.

Now, you think then what is different between static and singleton. the main difference between those two is singleton provide you object whereas static classes provide you with a static method. Singleton can implements interfaces, inherit from other classes and it is aligned with the object-oriented concept. Also, a singleton object can be passed to other methods as a reference whereas a static object cannot be passed as a reference.

For implementing singleton pattern to your code you must consider two common steps

  1. Making default constructor as a private so that it cannot be instantiated outside of the class.
  2. Create public creator method, which will create an object using a private constructor and save it on a static variable. so whenever any user requested for the object using public creator method it will return cached object from a static variable.

Top level view of a singleton design pattern class diagram


Following is sample code which ensures you only one object of the class is created and provide you access to that object using GetInstance() method.

public class Singleton {
        /// <summary>
        /// Static field to stored instance of the singleton class
        /// </summary>
        private static Singleton _instance;

         /// <summary>
         /// Private construct to restrict user to instance outside the class
         /// </summary>
         private Singleton() { }

         /// <summary>
         /// GetInstance method which create new instance if its null, and return instance  
         /// </summary>
         public static Singleton GetInstance => _instance ?? (_instance = new Singleton());

Following are some of the example of a real-world scenario where we can use singleton design pattern

  • Logging management
  • Device management
  • File management
  • Application configuration management
  • Cache management
  • Session-based shopping cart management

I have implemented a small project which demonstrates how we can use the singleton pattern for Logging to file or to external resources. find below git repository link for this.

Github: Singleton Pattern Example

2 thoughts on “Singleton Design Patterns

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s