Singleton Design Pattern Behavior

Anuradha Gunasinghe
4 min readMay 22, 2021

--

What is the Singleton Design Pattern?

The Singleton design pattern ensures that there is only one object of a class is created in the Java Virtual Machine and it restricts a class to instantiate its multiple objects. It is just a way to define a class. It is used in complex projects so that a class can have only one object in the complete execution of the project. A Singleton class provides a global point of access to it. Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects. Singleton means one instance for container.

Why we need singleton class?

Suppose you are working on a big project and using a complex object with data. There are many points when you need to get and put data in an object. There may be a chance, if you (Or new developer) get an object of the class by mistake, then it will return a new object with blank data. But in the case of the singleton class, it retrieves a singleton object, that is common and global from each point.

A non-singleton class can have different object from different ways. But a singleton class provides only one object by a single way. You can get object by any other way.

Eager initialization

In eager initialization, the object is created at the time of class loading. But it has a drawback that object is always created in memory whether it is being used or not. It is also known as the early initialization of a singleton object.

In above example, we are creating a singleton class Student. The object of Student(singleton class) automatically created during class loading. We can get the object by use of getInstance() method.

Static block initialization

The implementation of static block initialization is very similar to eager initialization. It initializes the object in the static block and also handling the exception at creation time. But still, it is not a best practice because objects always created during class loading.

Lazy Initialization

In eager initialization and static initialization, the object was created during class loading but in Lazy initialization, it restricts the creation of the instance until it is requested for the first time. Here the object of the singleton class is created in the getInstance() method only if the object is not already created.

Thread Safe Singleton

Above patterns are not thread-safe. Here we will discuss the way to create a thread-safe singleton class. We have to use the synchronized keyword with the global access method. So that only one can enter at a time. We can avoid such types of the situation by use of double checked locking. In this approach, we use a synchronized block instead of a synchronized method. We use the synchronization block inside the if statement with an additional check to ensure that only one instance of a singleton class is created.

--

--

Anuradha Gunasinghe

Software Engineer @ WTS, Bachelor of Engineering (BEng) Honours in Software Engineering Graduated from University of Westminster