Introduction to Singleton Design Pattern

“Only one instance of the class per container”

Singleton Pattern belongs to Creational Design Pattern. In this article, we will learn about the Singleton design pattern’s concepts, different approaches to apply it, and some best practices for using Singleton.

  • Lazy Initialization Method
  • Thread Safe Singleton Method.
  • Double Lock Method.
  • Lazy Load Method.
  • Static Block Initialization Method.
  • The only instance of the class has a private static variable of the same class.
  • This is the global access point to access the instance of the singleton class (it is a public static method that returns the instance of the class).
Example class diagram the singleton pattern (Source:

1. Eager Initialization

This is the simplest and easiest way to create a singleton class. According to this implementation, an instance of a singleton class is created by JVM when the class is loaded into the memory.

  • if your singleton class is not using a lot of resources

2. Static block initialization

Static block initialization is very similar to Eager initialization. The only difference is We have exception handling here because we do the object creation in a static block.

3. Lazy initialization

In this implementation, an object is created only when necessary (So the resource wastage is prevented).

4. Double-checking Singleton

The way to implement a thread-safe singleton is by using double-checking. This is also called double locking.

The output of double-check Singleton class


Associate Software Engineer at Virtusa