Introduction

Sometimes it’s important to have only one instance for a class. Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves. It involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.

Implementation

Different approaches of Singleton pattern implementation and design concerns with the implementation.

  • Eager initialization
    Instance of Singleton Class is created at the time of class loading, but it has a drawback that instance is created even though client application might not be using it.

    public class EagerInitializedSingleton {
        
        private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
        
        // private constructor to avoid client applications to use constructor
        private EagerInitializedSingleton(){}
    
        public static EagerInitializedSingleton getInstance(){
            return instance;
        }
    }
  • Lazy Initialization
    Lazy initialization method to implement Singleton pattern creates the instance in the global access method. The above implementation works fine in case of single threaded environment but when it comes to multi threaded systems, it can cause issues if multiple threads are inside the if loop at the same time. It will destroy the singleton pattern and both threads will get the different instances of singleton class.

    public class LazyInitializedSingleton {
    
        private static LazyInitializedSingleton instance;
        
        private LazyInitializedSingleton(){}
        
        public static LazyInitializedSingleton getInstance(){
            if (instance == null){
                instance = new LazyInitializedSingleton();
            }
            return instance;
        }
    }
    
  • Thread Safe Singleton
    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 static ThreadSafeSingleton getInstanceUsingDoubleLocking(){
        private static ThreadSafeSingleton instance;
        
        if (instance == null){
            synchronized (ThreadSafeSingleton.class) {
                if(instance == null){
                    instance = new ThreadSafeSingleton();
                }
            }
        }
    
        return instance;
    }

Important Points

  • Be careful if you are using multiple class loaders; this could defeat the Singleton implementation and result in multiple instances.
  • Singleton class constructor is private. You can’t extend a class with a private constructor. If you are using a large number of Singletons in your application, you should take a hard look at your design. Singletons are meant to be used sparingly.
  • The Singleton Pattern ensures you have at most one instance of a class in your application.