i was reading a book head first design pattern, actually read that book many times, an excellent book. Sharing my details idea singleton design patter.
class diagram of Singleton design pattern

a singleton class is look like that
public class Singleton
{
private static Singleton UniqueInstance; //variable to hold one instance of the class Singleton
private Singleton(){}
public static Singleton getInstance()
{
if(UniqueInstance==null)UniqueInstance=new Singleton();
return UniqueInstance;
}
}
here the Singleton class constructor is private and it has only one static method to get it, that ensures that this class has only one instance, and provide a global point to access it.
Now think for multi threading…
what will happen??? suppose you have 2 threads, so 2 different instance of the object will be created.
WHATS THE SOLUTION ???
add keyword to getInstance(), so it will ensure that 2 threads will not enter in the method at the same time. So solution codes looks like that.
public class Singleton
{
private static Singleton UniqueInstance;
private Singleton(){}
public static synchronized Singleton getInstance()
{
if(UniqueInstance==null)UniqueInstance=new Singleton();
return UniqueInstance;
}
}
but Remember that Synchronization is EXPENSIVE.
lets use “double-checked locking” to reduce the use of Synchronization in getInstance()
public class Singleton
{
private volatile static Singleton UniqueInstance;
private Singleton(){}
public static Singleton getInstance()
{
if(UniqueInstance==null)
{
synchronized (Singleton.class)
{
if(UniqueInstance==null)
UniqueInstance=new Singleton();
}
return UniqueInstance;
}
so this check for an instance and if there in not one, enter a synchronized block.
and volatile keyword ensures that multiple threads handle the uniqueInstance variable perfectly.
so hope that now you can perfectly implement Singleton design pattern in you application.


To be threadsafe for a class a simple implementation required would be:
public class Singleton
{
private static Singleton UniqueInstance;
private static Object syncLock = new Object();
private Singleton()
{
}
public static Singleton GetInstance()
{
lock(syncLock)
{
if (UniqueInstance == null)
{
UniqueInstance = new Singleton();
}
}
return UniqueInstance;
}
}
i presume, lazy loading is not your intention.
i would write the following code which ensure thread safety as well as less code.
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() { }
public static Singleton getInstance() {
return INSTANCE;
}
}
this work fine with java, but don’t have any idea about .NET. pretending it should work.
btw, as in java, we are not extensively using singleton pattern rather we all are using dependency injection pattern.
let’s say the above class singleton won’t keep INSTANCE variable any more.
rather our dependency injection framework will manage it’s instance and will inject through setter method of our required class.
let’s say.
class ServiceA {
// do bla bla bla
}
class ServiceAConsumer {
private final ServiceA mServiceA;
public ServiceAConsumer(final ServiceA pServiceA) {
mServiceA = pServiceA;
}
// do my job with ServiceA instance
// ie.
}
without DI framework we would do the following code –
class ServiceAConsumer {
private final ServiceA mServiceA;
public ServiceAConsumer() {
mServiceA = ServiceA.getInstance();
}
// do my job with ServiceA instance
// ie.
}
as you see we are binding our dependency in implementation level, let’s say ServiceA is an interface and ServiceAImpl is the concrete implementation. so you would write ServiceAImpl.getInstance(). when you have many services like this, it will become hard to maintain. thats why we are giving more priority on DI.
i think you will dig more about this area.
btw nice write up. keep up your good works.
Hi hasan,
Thanks for your nice idea sharing and comments.
In c# we can use sealed instead of final. But it’s not recommended to use as lazy loading what already mentioned.
Thanks to share your DI framework idea with me, I did not know about that.