Singleton Pattern

Characteristics of Singleton

Examples of 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); // [email protected]

    Singleton secondInstance = Singleton.getInstance();
    System.out.println(secondInstance); // [email protected]
  }

}

Disadvantages of Singleton

To access the full working code sample, click here

References

Singleton Design Pattern | Implementation and Disadvantages | Clean Code Series

SUBSCRIBE TO NEW CONTENT

Tutorials on System Design, BlockChain, Design Patterns and more sent out once a week.

Loading comments...