Wednesday, October 12, 2016

Singleton Pattern

Singelton pattern restricts the instantiation of a class and ensures that only one instance of the class exists per JVM.

To implement Singleton pattern, we have to follow the following step.

1. Constructor should be private.
2. Public static method that returns the instance of the class.

Lazy Initialization:

When you call the getInstance method then the object of the singleton class is created.

public class SingletonPattern {

private static SingletonPattern instance;

private SingletonPattern() {
}

public static SingletonPattern getInstance() {
if (instance == null) {
instance = new SingletonPattern();
}
return instance;
}
}

Thread-Safe Singleton:

In the case of thread safe singleton, we use the synchronized block or synchronized method to create the thread safe singleton class.
Below example is also known as double check locking. In this approach ,the synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created. 

public class ThreadSafeSingleton {

    private volatile ThreadSafeSingleton instance;
    
    private ThreadSafeSingleton(){}
    
    public static ThreadSafeSingleton getInstanceUsingDoubleLocking(){
        if(instance == null){
            synchronized (ThreadSafeSingleton.class) {
                if(instance == null){
                    instance = new ThreadSafeSingleton();
                }
            }
        }
        return instance;
    }
    
}


Bill Pugh Singleton Implementation:


In this approach when the singleton class is loaded, SingletonHelper class is not loaded into memory and only when someone calls the getInstance method, this class get loaded and create the singleton class instance.


public class Logger {

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


Note: Above approach doesn't require Synchronization.

Enum Singleton:

When we use the reflection then above all approach break and we are able  to create more than one object of each Singleton class to overcome this challenge we are using the enum singleton class.

public enum EnumSingleton {

     INSTANCE;  
}
Note:Only drawback of above approach is that it is not supporting the lazy loading.



 Note: When in distributed environment we serialize the singleton class and again deserialize it then it creates the new object to prevent this issue we implement the below method in our class.

protected Object readResolve() {
    return getInstance();
}


No comments:

Post a Comment