Creational Design Pattern: 1. Singleton pattern
By: Jhanak Didwania
Tags: Singleton Pattern, Software Development, Design Patterns, Creational Patterns, Software Engineering

Creational Design Pattern: 1. Singleton pattern

Creational design patterns deals with creation or cloning new objects of a class. Cloning occurs when a similar kind of object already exist and instead of instantiating a new object, we clone the existing one.

The different ways of creating objects will greatly influence how a problem is solved. Different languages therefore impact what patterns are possible to use.

Singleton Pattern

Singleton pattern is used when we require only single instance of a class. For example, making DB connection in an application, print queue of the printer, using loggers as they require a lot of resources. In all those cases, if there are multiple instances, then it will create confusion as well as inconsistency.

This pattern is used when the instance is shared and accessible globally across the application. Making sure that access to shared resources is thread safe is one very good example of where this kind of pattern can be vital.The intent of a Singleton pattern is to provide global access to a class that is restricted to one instance.

Let’s see how the object is created

public class DummyClass{ DummyClass(){}

} DummyClass dc = new DummyClass(); //every time when this line is called a new object of DummyClass is created

Now, whenever an object of DummyClass is needed, it’s constructor is called and an object is created. This is called Eager initialization.

But, in singleton pattern, we require only single instance of a class. Say, if a DB connection is setup already then we don’t need to create a new DB connection and should use the already existing connection. Let’s see how to achieve it.

Here, we have limited the scope of the constructor to the class itself. So, to create the object of this class, getDBConnection() static method of the class has to be called. It will then check if the dbConnection object is null, then create a new connection object and return otherwise return the existing connection object.

This example demonstrate the concept of lazy initialization. That means, we are not creating the object of the class unless it is truly needed. Hence, this program is more efficient in terms of resources.

There are trade-offs to the Singleton design principle. If there are multiple computing threads running, there could be issues caused by the threads trying to access the shared single object.

Issues in the above implementation

Now, it is possible that two threads at the same time are calling the getDBConnection method and the object is not created yet. For both the threads, the dbConnection object will be null and they both will try to create a new instance and use that instead. It is therefore very important to make sure that a single thread can access the getDBConnection at a time so that multiple instances are not created.

To achieve a thread-safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time.

Only one thread can access this synchronized block of code at a time

You might be thinking that the problem is solved now. But, unfortunately no! To enforce the singleton property we put a lock on the static getDBConnection method but compromising its performance at the same time. When an object is already created, then also, multiple threads have to wait to get the existing instance of that object. Can we do something better? Let’s see.

As it is evident that, lock is only required when the instance is null so that different threads do not create multiple instances. So, we can put a synchronized condition inside the if condition where we are checking whether the instance is null or not, and add a double check to eliminate the scenarios of multiple instances.

In real use, the singleton pattern can be implemented in different ways. It just defines the basic purpose this pattern is serving but the code implementation may differ.