Singleton Design Pattern

The name is singleton design pattern it self tells us regarding what exactly this design pattern will do. Using singleton design pattern we can restrict the object or instance creation only to one instance. So using singleton design pattern only one instance/object of a class will get created.

Theoretical we will maintain only one instance of a class inside class. A simple singleton class will have variables, methods and constructor as per below diagram.



A simple singleton class will have a instance variable which will maintain the single instance of a class, the reason behind private constructor is that it will not allow object creation outside the class and finally the public get instance method to fetch the instance. Now let's see the java example for same.

 
public class SingleTon {
	
	private static SingleTon instance= new SingleTon();
	
	//private allowed object creation only inside the class
	private SingleTon() {

	}
	
	public static SingleTon getInstance() {
		return instance;
	}
	
}

As you can see in the class, the getInstance() method is returning the static instance created while class is getting loaded. private constructor will restrict the object creation only inside the class.So outside class , it will not able possible to instantiate the object and whenever the object is needed it will be possible to retrieve it using the getInstance method.

Above was a design of the normal singletone class, where the design is goo for single thread environment.The challange will come when there is a question of good design architecture for multithreaded environment.

When it comes with multithreading for singleton class, most common answer is the define the getInstance() method as synchronized method , further more people might come up with a thought of portion of code as synchronized.The problem of making method it self as synchronized is that, at a time only one thread will be able to access the main portion of singletone class, so by that way its not a good design for multithreaded environment.

So for solving this we have double check mechanism, where we put 2 if condition.First one will be outside the synchronized block and one more will be there inside synchronize block.The first if condition is needed so that if the object is already created, we can directly return the created object. Second one is needed because if , first thread went inside the synchronized block when the object was not created.At the same time if second thread is passed the first if condition and went inside the synchronized block then we need to check again , if we do not do that then object will get created again.So for resolving this we need 2nd condition as well inside the synchronized block.

 

public class SingleTon {
	
	private static SingleTon instance;
	
	//private allowed object creation only inside the class
	private SingleTon() {

	}
	
	public static SingleTon getInstance() {
		if (instance == null) {
                    synchronized (SingleTon.class) {
                        if (instance == null) {
                          instance = new SingleTon();
                        }
                    }
                }
              return instance;
	}
	
}

Share this:

CONVERSATION

0 comments:

Post a Comment