System Design ( Day - 46 )

Singleton Design Pattern
The Singleton is a creational design pattern whose purpose is to ensure that a class has exactly one instance in the entire application and to provide a global point of access to it. whenever any part of the code needs that object, it gets the same instance rather than creating a new one every time.
public class Singleton {
private static Singleton instance; // initially null
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) { // first check
instance = new Singleton(); // create only on first call
}
return instance;
}
}
Making class Thread Safe
With the above method when multiple threads are trying to create a new object then it can able to create so that is a problem because we need to allow them to create only one Object throughout the application.
So That’s why we have to lock so that only one thread can access at a time, for the in java we have Synchronisation .
public class Singleton {
private static Singleton instance;
private Singleton() { }
// Synchronized ensures only one thread can enter at a time
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Double Checking
public class Singleton {
private static volatile Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) { // First null check (no lock)
synchronized (Singleton.class) { // Acquire lock
if (instance == null) { // Second null check (protected)
instance = new Singleton();
}
}
}
return instance;
}
}
Explanation
First Check (
if (instance == null)
): Skip locking if instance already exists.Synchronized Block: Only threads entering here attempt to create the instance.
Second Check: Re‐check inside the lock to ensure another thread didn’t already create it.
volatile
Keyword: Prevents instruction reordering so that onceinstance
is assigned, other threads see a fully initialized object.
Eager Initialisation
Before doing anything we have to create a new object and save it in the static variable whenever we are calling getInstance then we have to give that instance there no need of locking unlocking and double checks and all we can directly get the old instance.
Practical Use Case
1. Logging System
2. Database Connection
3. Configuration Manger
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
