Singleton Design Pattern

1 minute read

Singleton Pattern -> restricts the instantiation of a class to one object.

  • Ensure a class only has one instance and provide a global point of access to it.Eg: DB Connection needs to be a singleton
  • It is used to control the number of objects created by preventing external instantiation and modification.
  • This is useful when exactly one object is needed to coordinate actions across the system.

How ?

  • private constructor - so that no other class can instantiate a new object.
class SingletonDemo {
    ....
    private SingletonDemo() {
    }
}
  • private reference or instance - no external modification.
    //private static SingletonDemo singletonDemo = new SingletonDemo();//Eager Evaluation
    private static SingletonDemo singletonDemo;//Eager Evaluation
  • public static method (get()/of()/getInstance() - convention) is the only place that can get an object.

Eager Evaluation

//Lazy mode
public static SingletonDemo of(){
    if(null == singletonDemo){
        System.out.println("First time call");
        singletonDemo = new SingletonDemo();//Preventing from creating multiple instances
    }
    return singletonDemo;
}

Lazy Evaluation

private static final SingletonDemo singletonDemo = new SingletonDemo();//Eager Evaluation

There are four different ways to create objects in java:

  1. Using new keyword
  2. Using Class.forName()://reflection
  3. Using clone():
  4. Using Object Deserialization: Using new Intance() method

Private constructor doesn’t protect from instantiation via reflection.

Joshua Bloch (Effective Java) proposes a better implementation of Singleton

//This ENUM acts as a singleton bean
public enum SingletonDemoEnum {
    //INSTANCE;
    INSTANCE();

    public String doSomething(){
        return "Do something inside Enum";
    }
}

Access thew singleton object via

SingletonDemoEnum object = SingletonDemoEnum.INSTANCE;//TODO: Whats is INSTANCE

What does Spring use