Singleton Design Pattern Implementation in Java with an Example

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

  1. Make the constructor private
  2. 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

  1. Eager Initialization Method: In this method, at the time of class loading new instance is created.
  2. Lazy Initialization Method: In this approach, an instance will be created, when the first-time static method is called.
  3. Thread-safe singleton method: In this approach, at any point in time, a single thread is executing the method.

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 this method, the singleton variable is assigning with the null value. When the first-time we call the getsingleton() method, it will create and return a new object to the singleton variable. Now the value of the singleton variable changed from null to a 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:

Leave A Reply

Your email address will not be published.