Thursday, July 19, 2007

Singleton Pattern - "Guaranteed Single Object Model"

Introduction:
This blog only deals with implementing Singleton Pattern in Java.

Why? Whats the need?
Before we start, we must first understant the need of "single object model".
Say you want to access the printer/stream or any other resource then one would like to have a single handler for that resource to have greater controlling on its operations.
Or say you have an application accessing database, is it fine if all the modules of application access database with their own driver, in such conditions the singleton pattern is best suited.

Design Pattern and Singleton Pattern
Before we start our journy to singleton, lets first have a brife introduction about Design Patterns.
"Design Patterns are some guidelines, ganral rules, solution templates for common recurring problems"
And Singleton Pattern is a Creational Design Pattern - as it address and control cration of the object in question.
Definition: "A Design Pattern in witch at any time there is exactly one object/handler for any Class/Module"

Implementing Singleton in Java
According to definition for any class to be singleton there must not be more than one instance at any time, so first of all we must find a way to restrict users to create more instances of the object. This could be done my just making a private constructor, and providing a static method to access the instance of the object.

public class MySingleton{
private static MySingleton instance=null;
private MySingleton(){
//no code required - or any other initialisation code
}
public static MySingleton getInstance(){
if(instance==null){
instance=new MySingleton();
}
return instance;
}
//any other methods or code
}

So now no other class can create objects of this class and there can't be more than one instance, as we guarented in getInstance method. So is the job done? well yes and no - yes as it fulfills the definition and promery requirements and No as threading may create problems, and may lead to more than one instances.
Ohh thats now pretty easy, yeah we all know how to restrict threads to ruin all we have done, we all are smart people ;)
so just add synchronized keyword in the getInstance method, so our method signature becomes

public static synchronized MySingleton getInstance()

Well is it done yet? - Nop still one thing is there to trouble us, what if someone, someone having personal problem with your code ;), tries to make clone of your object, well you haven't given any clone method, but it is already there, remember Object.clone, and worst of all it has protected access so any subclass can be written and clone can be overridden, so just to avoid it add one clone method also.

So now we have our Singleton class with guaranted single object modle. Complete code for the class is below.

public class MySingleton{
private static MySingleton instance=null;
private MySingleton(){
//no code required - or any other initialisation code
}
public static synchronized MySingleton getInstance(){
if(instance==null){
instance=new MySingleton();
}
return instance;
}
public Object clone() throws CloneNotSupportedException{
throw new CloneNotSupportedException();
}
//any other methods or code
}

No comments: