Java -Singleton class-Singleton Design pattern-Why?How?

Singleton class means you can create only one object for the given class. You can create a singleton class by making its constructor private so that you can restrict the creation of the object. Provide a static method to get the instance of the object, wherein you can handle the object creation inside the class only. 

Advantage of Singleton design pattern

Saves memory because the object is not created at each request. Only a single instance is reused again and again.

Usage of Singleton design pattern

Singleton pattern is mostly used in multi-threaded and database applications. It is used in logging, caching, thread pools, configuration settings, etc.

How to create a Singleton design pattern?

To create the singleton class, we need to have a static member of the class, a private constructor, and a static factory method.

Static member: It gets memory only once because of static, it contains the instance of the Singleton class.

Private constructor: It will prevent to instantiate the Singleton class from outside the class.

Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.


There are two forms of singleton design pattern

Early Instantiation: the creation of instance at load time.
Lazy Instantiation: the creation of instance when required.


Early/Eager Instantiation

In such a case, we create the instance of the class at the time of declaring the static data member, so the instance of the class is created at the time of classloading.

Another thing that we need to keep in our mind is that Eager instantiation is thread-safe. Thread-safe means, it doesn’t matter how many threads are trying to invoke getInstance() method, they’ll always get that one object which is created eagerly. Hence, there is no chance that two objects will be created and there will be no violation of Singleton Design Pattern’s principle.

Let's see the example of a singleton design pattern using early instantiation.
public class Singleton {
private static Singleton instance = new Singleton();

private Singleton() {
System.out.println("Singleton being initialized");
}

private static Singleton getInstance() {
return instance;
}
}


Lazy Instantiation

Lazy Initialization is a technique where one postpones the instantiation of an object until its first use. In other words, the instance of a class is created when it's required to be used for the first time. The idea behind this is to avoid unnecessary instance creation. But there are concerns related to using such approaches in a concurrent scenario. But before that let's see how lazy initialization looks:
public class Singleton {
private static Singleton instance = null;

private Singleton() {
System.out.println("Singleton being initialized");
}

public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}

In a multithreaded environment, it may break singleton property.


To make a singleton class thread-safe, getInstance() method is made synchronized so that multiple threads can’t access it simultaneously.
public class Singleton {
private static Singleton instance = null;

private Singleton() {
System.out.println("Singleton being initialized");
}

synchronized public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}


Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String