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 as private, so that you can restrict the creation of the object. Provide a static method to get instance of the object, wherein you can handle the object creation inside the class only. 

Advantage of Singleton design pattern

Saves memory because object is not created at each request. Only 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.

There are two forms of singleton design pattern




Early Instantiation: creation of instance at load time.

Lazy Instantiation: creation of instance when required.


How to create Singleton design pattern?

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

Static member: It gets memory only once because of static, itcontains 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.

Early/Eager Instantiation

In such case, we create the instance of the class at the time of declaring the static data member, so 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 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 a object until its first use. In other words the instance of a class is created when its 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 lets 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 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;
}
}


 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments