Overview Of Singleton Design Pattern

Arshad Suraj
Geek Culture
Published in
3 min readMay 22, 2021
Image by — google

What is Singleton?

The Singleton is one of the most used design pattern which is used to create a class that can have only one instance that can be accessed globally. Singleton is classified as a creational pattern.

Only one singleton object can be made, per container. If you’re a JAVA developer, you’ll get one Instance per JVM.

Implementation

Class diagram of Singleton Design pattern

Things to Keep in Mind:

  • Singleton class must have a private constructer.
  • Singleton class must contain a static instance of itself.
  • Singleton class must have a static method to access Its Instance globally.

The singleton design pattern is used in two forms. They are,

  • Early Instantiation: instance will be created at the load time.
  • Lazy Instantiation: Instance will be created when required.

Let’s look at each,

Early Instantiation

Here Singleton object will be created at the time of classLoading, and the same object will be returned for every access to it.

Assume we are developing a Java Application to a school. Therefore, A School can have only one principal. As a result, a singleton class is used to represent a principal.

code snippet of Singleton class
Code snippet of main method

The output for the above codes are below,

Output of the Earlier Instantiation

Since it is a Singleton Object, each invoke will return the same object.

Lazy Instantiation

Lazy Instantiation is the most recommended way of following a singleton pattern. Here the instance of the class is created when required, and the same object will be returned for every access to it. Let’s look at the code of Lazy Instantiation for the above same example.

Code snippet of Lazy instantiation
Code snippet of managing threads

The output for the above codes are below,

Output of the Lazy instantiation

Here both threads are getting the same output, since they are accessing a singleton object.

Things to notice on the implementation of Lazy Instantiation:

  • Throwing a RuntimeException inside the private constructor is a good practice. Because reflection frameworks may interfere and create a second instance by manually invoke the constructor. As a result, we can prevent this by throwing an exception.
  • we must put a synchronized block within the getInstance() method Ex:getPrincipal()to ensure this code is thread-safety. This will put a synchronized lock for threads when concurrent accessing.
  • we must check twice whether Singleton object is null, within the getInstance() method. This is called double-checking. If we only check once, while one thread is creating the object (actually the object is not created yet, it is being created), another thread may get checked during that time and enter within “if condition block” which checks whether the singleton object is null. As a result, second thread may create a second object when it gets chance.
  • changing the getInstance() method as the synchronized method is a bad practice. Since the current thread will block all other thread’s execution, until it completes the execution of all lines within the method, other threads may wait for long-time.

NOTE: When we creating a singleton object, no parameters should be used. If parameters are needed, it’s better to follow the factory method design pattern instead of the singleton design pattern.

Advantage of Singleton design pattern

  • Since an object is not produced for each request, memory is saved. Just one instance is used again and again.

Real-Time examples where Singleton design pattern is used

  • Logging
  • Caching
  • Thread pools
  • Configuration settings (Ex: database configuration class can be used as singleton class)

Keep Learning ❤️

--

--