How to create a Singleton class



What is Singleton Class?
1. If a class has only one instance at one point of time, it is called singleton class
2. The name itself indicates (singleton - one)
3. Singleton is a simplest design pattern in java
How to make a class singleton
1. To make a class singleton we have to restrict the class object creation more than once then only it can be called as singleton class...
2. So to restrict object creation in sub classes we have to make the constructor private
3. Since we need to have single object across environment we have to
4. Use Private static variable to create instance of the class.
5. Now to make the created object accessible in sub Classes we have to provide a public static method (getInstance()) which returns the
created object, instead of creating new object.
6. Singleton pattern is used for logging, drivers objects, caching and thread pool
There are different approaches of Singleton pattern implementation and design concerns with the implementation.
1.Eager initialization
2.Static block initialization
3.Lazy Initialization
4.Thread Safe Singleton
5.Enum Singleton
Eager initialization
In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton classbut it has a drawback that instance is created even though client application might not be using it.
If your singleton class is not using a lot of resources, this is the approach to use. But in most of the scenarios,
Singleton classes are created for resources such as File System, Database connections etc and we should avoid the instantiation until unless client calls the getInstance method. Also this method doesn’t provide any options for exception handling.
Singleton classes are created for resources such as File System, Database connections etc and we should avoid the instantiation until unless client calls the getInstance method. Also this method doesn’t provide any options for exception handling.
Static block initialization
Static block initialization implementation is similar to eager initialization, except that instance of class
is created in the static block that provides option for exception handling
Both eager initialization and static block initialization creates the instance even before it’s being used and that is not the best practice to use. So in further sections, we will learn how to create Singleton class that supports lazy initialization.
Lazy Initialization
Lazy initialization method to implement Singleton pattern creates the instance in the global access method.
Here is the sample code for creating Singleton class with this approach.
Here is the sample code for creating Singleton class with this approach.
The above implementation works fine in-case of single threaded environment but when it comes to multi-threaded systems, it can cause issues if multiple threads are inside the if loop at the same time. It will destroy the singleton pattern and both threads will get the different instances of singleton class. In next section, we will see different ways to create a thread-safesingleton class.
Thread Safe Singleton
The easier way to create a thread-safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time.
General implementation of this approach is like the below class.
General implementation of this approach is like the below class.
Above implementation works fine and provides thread safety but it reduces the performance because of cost associated with the synchronized method, although we need it only for the first few threads who might create the separate instances .To avoid this extra overhead every time,double checked locking principle is used. In this approach,
the synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created. Below code snippet provides the double checked locking implementation.
the synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created. Below code snippet provides the double checked locking implementation.
Enum Singleton
Joshua Bloch suggests the use of Enum to implement Singleton design pattern as Java ensures that any enum value is instantiated only once in a Java program.
Since Java Enum values are globally accessible, so is the singleton.
The drawback is that the enum type is somewhat inflexible;
for example, it does not allow lazy initialization.