Singleton Design Pattern | Implementation

Single Design Pattern is one of the creational style of design pattern. It is often used in scenarios where the class needs only one instance. One of the very common examples which we come across is the Database connection class, when we only need one connection to be shared among other Objects as creating multiple connections/separate connection for every other Object is a costly operation, which would slow down the operations.

How do you design a Singleton Class :

  • Make constructor as private
  • Write a static method which returns Object of same Singleton class. Here, the concept of Lazy initialization in used to write this static method.

How is Singleton Class different from Normal Class?

When instantiating a class, a normal class uses constructor where as a Singleton class will need a static method to create an instance of a class that too only if it is null i.e it is not already in use.

Different levels of making a class Singleton

Method 1: Classic Implementation also called lazy instantiation

class Singleton
{
    private static Singleton obj;
  
    // private constructor to force use of
    // getInstance() to create Singleton object
    private Singleton() {}
  
    public static Singleton getInstance()
    {
        if (obj==null)
            obj = new Singleton();
        return obj;
    }
}
            

Method 2: make getInstance() synchronized

// Thread Synchronized Java implementation of 
// singleton design pattern
class Singleton
{
    private static Singleton obj;
  
    private Singleton() {}
  
    // Only one thread can execute this at a time
    public static synchronized Singleton getInstance()
    {
        if (obj==null)
            obj = new Singleton();
        return obj;
    }
}
            

The main disadvantage of this is method is that using synchronized every time while creating the singleton object is expensive and may decrease the performance of your program

Method 3: Eager Instantiation

// Static initializer based Java implementation of
// singleton design pattern
class Singleton
{
    private static Singleton obj = new Singleton();
  
    private Singleton() {}
  
    public static Singleton getInstance()
    {
        return obj;
    }
}
            

Method 4 (Best): Use “Double Checked Locking”

// Double Checked Locking based Java implementation of
// singleton design pattern
class Singleton
{
    private volatile static Singleton obj;
  
    private Singleton() {}
  
    public static Singleton getInstance()
    {
        if (obj == null)
        {
            // To make thread safe
            synchronized (Singleton.class)
            {
                // check again as multiple threads
                // can reach above step
                if (obj==null)
                    obj = new Singleton();
            }
        }
        return obj;
    }
}              
            

Advantages of Singleton

Saves memory because object is not created at each request. Only single instance is reused again and again.

Summary

In this article we learnt about Singleton Design Pattern and its Implementation.
Hope you liked the article !


Leave a Comment