Tutorial List
Home
Interview Questions
Interview
Interview Questions
Links
Web Home
About Us

Singleton Pattern - One For All Object

Singleton pattern is one of very basic and most widely used design pattern. You might have used Singleton pattern without knowing about it. If you are aware of struts framework, Action class (infact Child of Action Class) is Singleton. Action class is made singleton by Struts framework itself without your intervention but in your application design, you need to make sure which class is need to be Singleton.
Singleton pattern ensure there is one and only one instance (Object) of a Class exist at a time, in other words when you want only one object to be exist of some class, make is Singleton.

How to Implement Singleton Pattern,
Singleton pattern can be implemented in numerous ways but there are three standard way to implement singleton pattern.

1. Synchronized Method:
******************************************************************
Class MyClass
{
private static MyClass objSingleton = null;

/* to make sure class can’t be instantiated outside */
private MyClass()

/*this method returns singleton object*/
public static synchronized MyClass getInstance()
{
If ( null == objSingleton ) {
objSingleton = new MyClass();
}
return objSingleton;
}
//… Other Member of Class
}
******************************************************************
That’s all. Doesn’t it look simple to make MyClass singleton! Well all you need to do is create a private static object to make is make its scope global within the class. Then you need to make its constructor private so it cannot be instantiated from outside. Finally make a public static method to return the single instance of the object. Don’t forget to make the method synchronized to make it thread safe.
This approach is simple but as we know, make a method synchronized is really expensive in multi-threaded environment, so if your singleton object is need to used very frequently by many objects, abstain from this approach and read further ?…

2. Early Instantiation:
******************************************************************
Class MyClass
{
private static MyClass objSingleton = new MyClass();
/* to make sure class can’t be instantiated outside */
private MyClass()
/*this method returns singleton object*/
public static MyClass getInstance()
{
return objSingleton;
}
//… Other Member of Class
}
******************************************************************
This approach is most simple. You need to instantiate the object when you declared it, and forget everything except returning object from a static method. This approach is not encouraged much as it instantiate object before it is actually accessed but this will improve your access time to singleton object.

3. Double Lock Checking:
This will introduce some extra lines in your code but this is considered as best approach and endorsed in many case studies to implement singleton.
******************************************************************
Class MyClass
{
private volatile static MyClass objSingleton = null;
/* to make sure class can’t be instantiated outside */
private MyClass()
/*this method returns singleton object*/
public static MyClass getInstance()
{
If (null == objSingleton ) {
synchronized (MyClass.class) {
If (null == objSingleton ) {
objSingleton = new MyClass();
}
}
}
return objSingleton;
}
//… Other Member of Class
}
******************************************************************
This looks cumbersome but if you see, this is almost same as approach 1, except we are synchronizing over Class rather than Method. This reduced the synchronized codes hence improve the performance.

When to use singleton pattern:
Well this is not simple answer. This depends on your application design and your experience. You need to identify which all classes need to be singleton. A simple measure is, all class used as controller or handler class need to be singleton.
Few common scenarios are,
  • Data Source handling class
  • Property file handling class
  • Email handling class
  • Application Configuration class
Clone methos v/s Singleton pattern
When writing a class using the Singleton pattern, only one instance of that class can exist at a time. As a result, the class must not be allowed to make a clone. To prevent this, override the clone() method using the following code:

public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();

}

Please make sure you are using above implementation of clone method only if you have implemented Clone interface in your singleton class as there is no need to place redundant method in your class....

We will continue discussing design pattern in coming posts,
In case of any query, feel free to email me at mohit.amour@gmail.com

Thank You,
The Interpreter


No comments: