Saturday, May 21, 2016

Singleton - Master it in 10 mins .

Hi folks ,

Let's have some understanding on basic design pattern in any programming model known as the Singleton .This session is based on Singleton in Java. I hope you enjoy reading it and make the most out of it .

Singleton design pattern is one of the most common patterns you will see in Java applications and it’s also used heavily in core Java libraries. In this post i have written about almost everything i know and what possibly is out about Singleton.

Audience : If you are a Java beginner or an experienced person , this is for you . If you are an interviewer you can use this . If you want to prepare for an interview definitely read it .


  • What is singleton : Singleton is a class which has only one instance in whole application and provides a getInstance() method to access the singleton instance.
  • Any class which you want to be available to whole application and only one instance is needed in the lifetime of the application. The best example would be Runtime class ,  since on whole java application only one runtime environment can be possible making Runtime Singleton is right decision.  
Ways to write a singleton -

A). Lazy loading : Only when the client asks for the object , create and return it, otherwise don't bother.

public class Singleton {
    private static Singleton INSTANCE;

    public static Singleton getInstance() {
            if (INSTANCE == null) {
                INSTANCE = new Singleton();
            }
            return INSTANCE;
    }
}

B). Early loading : Here , Singleton instance is created when class is loaded into memory.

public class Singleton {
    private static final Singleton INSTANCE = new Singleton();

    public static Singleton getInstance() {
            return INSTANCE;
    }
}

C). When your application runs in a multi threaded environment the above code will create multiple instances of Singleton class , hence make this whole getInstance() method synchronized .

public class Singleton {
    private static Singleton INSTANCE;

    public static synchronized Singleton getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }
}


D). Though the above way is  thread-safe and solves issue of multiple instances , it's not very efficient. One that you need to bear cost of synchronization the entire time this method is called  even when  synchronization is only needed when Singleton instance is created.

We have the double checked locking pattern, where only critical section of code is locked. It's called double checked locking because there are two checks for INSTANCE == null , one without locking and other with locking (inside synchronized) block.

public class Singleton {
    private static Singleton INSTANCE;

    public static Singleton getInstance() {
        if (INSTANCE == null) { // single check
            synchronized (Singleton.class) {
                if (INSTANCE == null) { // double check
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }
}

Double checked locking should only be used when you have requirement for lazy initialization otherwise use Enum based Singleton .

In the above process unntil you make INSTANCE variable volatile , the code is broken !! WHY ???

Without volatile modifier it's possible for another thread in Java to see the half initialized state of INSTANCE variable, but the volatile keyword assures the happens-before relationship, all the writes will happen on volatile INSTANCE before any of its reads .


    private volatile static Singleton INSTANCE;

E). Enum Singletons are new way to implement Singleton pattern in Java by using Enum with just one instance.  Compared to double checked locking with synchronization , Enum singletons are cake walk ascreating of Enum instance is thread-safe by default.Lesser code and all you need to do is EasySingleton.INSTANCE to get the instance .


public enum Singleton {
    INSTANCE;
}

F). Initialize-on-demand :  This method relies on the JVM only initializing the class members on the first reference to the class. In this case, we have a inner class that is only referenced within the getInstance() method , initializing the INSTANCE.

public class Singleton {

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        static final Singleton INSTANCE = new Singleton();
    }
}

Now let's see how can singleton contract be broken and how can we avoid that.


  • Clone method : Do not implement Cloneable interface, if do so , throw an Exception from clone method with a message like Singleton already created .
  • Constructor / Reflection : Even though we can keep the constructor as private ,via reflection its possible to access the private methods and variables. Ensure to throw an exception if constructor gets called , as Singleton already cerated.  
  • Serialization : During serialization readObject() is used to create instance of the  class and it return new instance every time but by using readResolve() method, one can replace it with original Singleton instance. 

Now that's all about our old and famous Singleton design pattern  . I hope you learnt something new today and hopefully will not forget these points.

Happy Coding.

No comments:

Post a Comment