Singleton Pattern

Published: 2017-12-25 • Updated: 2019-09-09

Characteristics of Singleton

  • Private Constructor
  • Private Instance of the class
  • Static in nature
  • Thread safe
  • No parameters to the constructor

Examples of Singleton

  • Instance of Runtime (Runtime.getRuntime())
  • Spring beans (Default mode is singleton)

Java Implementaion

Singleton.class

public class Singleton {

  // Lazy loading
  // Volatile is necessary to avoid threads to see half initialized instance of Singleton
  private static volatile Singleton instance = null;

  private Singleton(){

  }

  /* Synchronizing at method level will synchronize every call to getInstance
   */
  /*public static synchronized Singleton getInstance(){

    if(instance == null){
      instance = new Singleton();
    }
    return instance;
  }*/

  /* We want to synchronize only when we are creating the object to avoid race condition
  Returning the created object should not be blocked
   */
  public static Singleton getInstance(){

    if(instance == null){
      synchronized (Singleton.class){
        // Double checking to make sure thread already inside first null check
        // doesn't create Race Condition
        if(instance == null){
          instance = new Singleton();
        }
      }

    }
    return instance;
  }
}

SingletonImpl.class

public class SingletonImpl {

  public static void main(String[] args) {

    Singleton instance = Singleton.getInstance();
    System.out.println(instance); // singleton.Singleton@610455d6

    Singleton secondInstance = Singleton.getInstance();
    System.out.println(secondInstance); // singleton.Singleton@610455d6
  }

}

Disadvantages of Singleton

  • Over Usage
  • Hindrance in writing Unit test
  • Confused with Factory design pattern (where we pass an argument to constructor for creating objects)

To access the full working code sample, click here

References

Design Pattern
Share this Post