Thursday, February 12, 2009

Singleton Design Pattern

Singleton Pattern:
This pattern is used to ensure that only one instance of a class is created which is accessible globaly in an application.

Implementation:
The definition of singleton pattern is probably the easiest among all design patterns. However its implementation requires a bit of care, as we will see here. Consider the below code:

Program 1:
public class MySingleton{
private static MySingleton instance = null;
private MySingleton() //This constructor will ensure that default constructor is not created
{
}
public static MySingleton getInstance()
{
if(instance == null)
{
instance = new MySingleton();
}
return instance;
}
}


Here we are declaring MySingleton as static to make it available globaly in an application. The constructor is declared as private so that any client creatin object of this class is bound to call getInstance() function instead of calling the constructor. Inside the getInstance() method we are creating the instance of MySingleton only if the instance is null. Thus once an instance is created, other instances of MySingleton class cannot be created.
Above implementation of Singleton pattern is correct to a large extent. However this implemenation may fail in certain scenarios as following:
1) If class is loaded by different classloaders.
2) If a Singleton object is serialized and then subsequently deserialized
3) If two "threads" call getInstance() method at the same time.

Here we will ignore the first two scenarios because I want to give you an opportunity to do research. Now let us consider the third scenario. I think by the time you have read these lines, and if you are a true Java nerd, your suggestion should be ready. The obvious suggestion is simply synchronise the method getInstance() something as following:

Program 2:
public class MySingleton{
private static MySingleton instance = null;
private MySingleton() //This constructor will ensure that default constructor is not created
{
}
public synchronized static MySingleton getInstance()
{
if(instance == null)
{
instance = new MySingleton();
}
return instance;
}
}


Brilliant !! ... so the issue is resolved and we have just implemented a singleton design pattern which is thread safe.
Though above implementation is thread safe but synchronisation is very expensive thing to do as far as performance is concerned. There is an easy and performance wise better approach to do this. See Program 3:

Program 3:

public class MySingleton{
public final static MySingleton instance = new MySingleton();
private MySingleton()
{
}
}


Since static member variables are guaranteed to be created, the first time they are accessed, so this implementation is thread safe. Both Program 2 and Program 3 have there own advantages/disadvantages. Program 2 is performance wise not good but it gives flexibility of changing the number of allowable instances. Suppose later you decided that i should allow three instances of this object to be created, all you will need is a change in the if condition of Program 2 without touching the client side program. Program 3 does not give this flexibility. But as we know when it comes to performance then Program 3 has a definite edge.

Examples of Singleton Design Pattern:
1) One file system, one window manager, one printer spooler, one Test engine, one I/O socket etc
2) Java serial port package viz javax.comm

No comments:

Post a Comment