The Singleton design pattern restricts to one instance. If we try to create a new instance, it only points to an already created instance. With normal class, we can able to create any number of objects we want.
The singleton design pattern ensures only one class instance in JVM. It is one of the four creational design patterns in Java. Unlike normal class instance, the static instance is not qualified for garbage collection.
Why to use singleton design pattern?
The singleton design pattern restricts the unnecessary creation of class instances in JVM. The design pattern creates only one instance and all new requests will access it. It saves the JVM memory and improves performance.
The common use of singleton design pattern is, logging, caching, and database access.
Recommended: Stack Data structure Implementation in Java using an Array
Rules to create Singleton class
There are certain rules to follow in order to create the singleton class
- Make the constructor private
- Create a static method that will return an instance of the class. When the method is called for the first time, it returns a new instance. After the first time, if we try to instantiate the Singleton class, the new variable points to the first instance created will return.
Implementation of Singleton class in Java
There are multiple way to design singleton class in Java
- Eager Initialization Method: New instance creates at the time of class loading.
- Lazy Initialization Method: Static method creates an instance after the first time it executes.
- Thread-safe singleton method: A single thread executes the method, at any point in time.
1. Eager Initialization Method
At the time of class loading, the getEagerSingleton() method returns instantiated singleton variable. This type of initialization is called eager initialization.
public class EagerSingleton {
//singleton class variable with instantiated
private static EagerSingleton singleton = new Singleton();
// private constructor
private EagerSingleton() { }
//returns the singleton variable
public static EagerSingleton getEagerSingleton() {
return singleton;
}
}
2. Lazy Initialization Method
In lazy initialization, the singleton variable initializes with a null value. The method getsingleton()
creates a new object when the method called the first time. Now singleton variable changes from null to the new object.
Next time, if we try to call the getSingleton()
method, it will return a singleton variable that is already assigned with an object.
public class Singleton {
//singleton class variable
private static Singleton singleton = null;
// private constructor
private Singleton() { }
//static method to return singleton instance of the class
public static Singleton getSingleton() {
if(singleton == null) {
singleton = new Singleton();
return singleton;
}
return singleton;
}
}
Thread safe Singleton Class
To create a thread-safe singleton class, declare the method as a synchronized method. This will restrict multiple threads to execute the method.
public class ThreadSafeSingleton {
private static ThreadSafeSingleton singleton = null;
//private constructor
private ThreadSafeSingleton() { }
//Synchronized method
public synchronized static ThreadSafeSingleton getThreasSafeSingleton() {
if(singleton == null) {
singleton = new Singleton();
return singleton;
}
return singleton;
}
}
Conclusion
Private constructor restricts new object creation, return a reference variable of the class with object assigned to it in a static method will make singleton class. It is an important design pattern that restricts unnecessary object creation.
Please write a comment for any modification and suggestion.
Recommended: