A guide To The Singleton Design Pattern
Posted By : Harshit Verma | 19-Apr-2018
The "Singleton design pattern" comes under the category of the " Creational Design Pattern". When a singleton comes to implementation, it contains a lot of implementation concerns in it.
The important characteristics of the Singleton Pattern are as follows:-
1). Singleton pattern helps to restrict the instantiating of a class, it also ensures that there is only one instance of the class per application.
2). The singleton class facilitates a global access point for getting the instance of the class.
3). Singleton pattern can be used for logging, drivers objects, caching and thread pool.
4). In other design patterns like Abstract Factory, Builder, Prototype, Facade etc Singleton design pattern is also used.
5). In core java classes also, for example, java.lang.Runtime, java.awt.Desktop singleton design pattern is used.
By using the below common concept the singleton design pattern is implemented:-
1). The private constructor is present in singleton class to restrict instantiating of the class from other classes.
2). A private static variable of the same class is used that is the only one instance of the class.
3). In singleton public static method is used, this method returns the instance of the
Here are some approaches
1). Eager initialization
2). Static block initialization
3). Lazy Initialization
4). Thread Safe Singleton
5). Bill Pugh Singleton Implementation
6). Using Reflection to destroy Singleton Pattern
7). Enum Singleton
8). Serialization and Singleton
Eager initialization:-
In eager initialization, the instantiating of Singleton Class is created at the time when the class is loading, can say at class loading time, but it also has a drawback that is instance is created even though client application might not be using it that creates an extra unused object.
public class EagerInitializedSingleton {
private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
//private constructor
private EagerInitializedSingleton(){}
public static EagerInitializedSingleton getInstance(){
return instance;
}
}
Static block initialization:-
The Static block initialization, it's process of implementation is same as of eager initialization implementation process, but the change is it excepts that instance of a class that is created in the static block facilitates the option for exception handling.
public class StaticBlockSingleton {
private static StaticBlockSingleton instance;
private StaticBlockSingleton(){}
//static block initialization for exception handling
static{
try{
instance = new StaticBlockSingleton();
}catch(Exception e){
throw new RuntimeException("Exception occured in creating singleton instance");
}
}
public static StaticBlockSingleton getInstance(){
return instance;
}
}
Lazy Initialization:-
Lazy initialization method that is used to implement Singleton pattern that creates the instance in the global access method. But when the application comes to
public class LazyInitializedSingleton {
private static LazyInitializedSingleton instance;
private LazyInitializedSingleton(){}
public static LazyInitializedSingleton getInstance(){
if(instance == null){
instance = new LazyInitializedSingleton();
}
return instance;
}
}
Thread Safe Singleton:-
The way to create a thread-safe singleton class is to make a method that is the global access and is in a synchronized
public class ThreadSafeSingleton {
private static ThreadSafeSingleton instance;
private ThreadSafeSingleton(){}
public static synchronized ThreadSafeSingleton getInstance(){
if(instance == null){
instance = new ThreadSafeSingleton();
}
return instance;
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Harshit Verma
Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.