Singleton Design Pattern

In this article lets discuss about Singleton Design Pattern.

In many applications there may be a need to create only one object for a class so that the object will be shared by the entire application code. In this case we need to make sure not more than one object to be created.

We can do this by making the constructor the class as private and returning the object using a static method. Have a look at the following Singleton class.

package in.techdive.java;

public class Singleton {
       
        private static Singleton s = null;
       
        private Singleton()
        {
        }
       
        public static Singleton getInstance()
        {
            if(s==null)
            {
                s = new Singleton();
            }
                return s;
        }
}

The getInstance() method returns the single instance of the Singleton class. If the object is not created, the method creates a new object and then returns the instance. Wow! Is it that simple to create a singleton class? Not so, are you able to see any chance of error in the above code?

Imagine what will happen when multiple threads access the Singleton class. There is a chance of more than one object being created.

For example, Thread 1 calls the getInstance() method, the method makes a null check and suppose if that Thread 1 is preempted and Thread 2 gets the control to call getInstance() method. It checks for null and creates a new instance, returns the object(1). Now, since Thread 2 has completed its job, after some time Thread 1 comes in to action and sees that the reference is null, so it creates another instance of Singleton class and returns it.

In this way there is a chance of creating more than one object in multithreaded environment. How do we avoid it? Have a look at the improved version of Singleton class.

package in.techdive.java;

public class Singleton {
       
        private static Singleton s = null;
       
        private Singleton()
        {
        }
       
        public synchronized static  Singleton getInstance()
        {
            if(s==null)
            {
                s = new Singleton();
            }
                return s;
        }

}

We have just made the getInstance() method as a synchronized one, so the method is thread safe in multithreaded environment.

Now take a closer look at the getInstance() method, the synchronized keyword adds more overhead, everytime the method is called. But it is actually required only when the first time the object is created. Is there any other better way to handle this without synchronized keyword? And the answer is yes, have a look at the better version of Singleton class.

public class Singleton {
       
        private static Singleton s = new Singleton();
       
        private Singleton()
        {
        }
       
        public  static  Singleton getInstance()
        {
                return s;
        }
}

Here we create the object of Singleton class, which is a static variable. So that its created whenever the class is loaded and there is no need for synchronization even in the multithreaded environment.

Wait a minute! Why do we need to complicate things for creating a single object. Why don't we have all the methods as static, so that we need not create an object at all?
Yes that would be better if you require a set of utility methods in an application. But remember, a class with static methods cannot be overridden in sub class.

Well! Can you think of any singleton class in Java? What about a Servlet, which is executed by a web container. The web container creates only one object for the servlet and multiple threads may access it. Its the job of the programmer to make the servlet class as thread safe.

For Further Study

Object Pooling using Singleton
Factory Design Pattern

Technology: 

Search