Concept of Singleton Class in Java [Detailed]

0
90

Singleton Class comes in the Creational Design Pattern. It is simple and very demanding topic among developers. Hence, we’ll see the principle, problems and different way to implement Singleton class.

What is a Singleton class in Java?

Singleton class design pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java Virtual Machine. And It must provide a global access point to get the instance of the class.

To design a singleton class:

  1. Make constructor as private.
  2. Write a static method that has return type object of this singleton class.

How to make Singleton Class in Java?

Java Singleton Design Pattern Best Practices with Examples
Java Singleton Design Pattern Best Practices with Examples
  • Private constructor to restrict instantiation of the class from other classes.
  • A private static variable of the same class that is the only instance of the class.
  • A public static method that returns the instance of the class, this is the global access point for the outer world to get the instance of the singleton class.

This is the easiest and simplest method to create a Singleton class But it has some drawback like:

  • If an object of Singleton Class is null. It throws NullPointerException.
  • It works for only single threaded application
  • An instance is created even though client application might not be using it.
public class Singleton{
  private Singleton(){}
  private static Singleton instance = new Singleton();
  
  public static Singleton getInstance(){
    return instance;
  }
}

We’ll resolve every possible drawback and make full-proof Singleton class but resolve every drawback step by step. So firstly we’ll resolve NullPointerException part. And we can resolve it by Lazy Initialization.

Lazy initialization method to implement Singleton pattern creates the instance in the global access method.

public class Singleton{
  private Singleton(){}
  private static Singleton instance;
  
  public static Singleton getInstance(){
    if(instance == null)
      instance = new Singleton();
    return instance;
  }
}

It resolves NullPointerException but above both codes will work for the single-threaded application, it can cause issues if multiple threads are inside the if loop at the same time. It break the law of singleton pattern and both threads will get the different instances of a singleton class.

Now let’s make thread-safe Singleton Class.

The easier way to create a thread-safe singleton class is to make the global access method synchronized so that only one thread can execute this method at a time.

public class Singleton{
  private Singleton(){}
  private static Singleton instance;
  
  public static synchronized Singleton getInstance(){
    if(instance == null)
      instance = new Singleton();
    return instance;
  }
}

It provides thread-safety but there have some drawback remains:

  • It reduces the performance because of the cost associated with the synchronized method.
  • An instance is created even though client application might not be using it.

To resolve above both issues by using Bill Pugh implementation where we use an inner static helper class.

Bill Pugh came up with a different approach to create the Singleton class using an inner static helper class.

public class Singleton{
  private Singleton(){}
  
  private static SingletonHelper{
    private static Singleton instance = new Singleton();
  }
  
  public static Singleton getInstance(){
    SingletonHelper.instance;
  }
}

This is the most widely used approach for Singleton class as it doesn’t require synchronization. I am using this approach in many of my projects and it’s easy to understand and implement also.

I hope this article helps you in grasping fine details of Singleton design pattern. But list is not ended here if you want to know more about Singleton Class then comment us or mail us at ceo.tellmehow@gmail.com

 

 

Share your thoughts

Loading Facebook Comments ...
Loading Disqus Comments ...