Singleton - Creational Design Pattern

Singleton - Creational Design Pattern

2019, Feb 10    

☝ Singleton

Ensures that only one object of a particular class is ever created in an execution environment. Example:

public class Singleton {
    private Singleton() {}
    private static class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }
    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

When to use? When you need some kind of a global state to represent in your application.