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
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;
}
}
More topics...
Comments
Post a Comment