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. It been always a controversial topic in developers.

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
class, so that the instance is available in the overall application.

Here are some approaches
on Singleton pattern implementation and design concerns having their implementation:-

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
multi threaded systems, it may cause issues if multiple threads running inside the loop at the same time.

 

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
way, so that it allows only one thread to execute this method at a time. But the execution of the one thread at a time increase the execution time and reduces the performance.
 

public class ThreadSafeSingleton {

private static ThreadSafeSingleton instance;

private ThreadSafeSingleton(){}

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

}

About Author

Author Image
Harshit Verma

Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..