Singleton
Singleton
Singleton Pattern
Singleton pattern restricts the instantiation of a class and ensures that only
one instance of the class exists in the java virtual machine.
The singleton class must provide a global access point to get the instance of
the class.
Singleton pattern is used for logging, drivers objects, caching and thread pool.
Singleton design pattern is also used in other design patterns like Abstract
Factory, Builder, Prototype, Facade etc.
Singleton design pattern is used in core java classes also, for
example java.lang.Runtime, java.awt.Desktop.
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
1. Eager initialization
private StaticBlockSingleton(){}
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 a Singleton class that supports lazy
initialization.
Read: Java static
3. 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.
private LazyInitializedSingleton(){}
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.
package com.journaldev.singleton;
private ThreadSafeSingleton(){}
public static synchronized ThreadSafeSingleton
getInstance(){
if(instance == null){
instance = new ThreadSafeSingleton();
}
return instance;
}
Prior to Java 5, java memory model had a lot of issues and the above
approaches used to fail in certain scenarios where too many threads try to get
the instance of the Singleton class simultaneously. So Bill Pugh came up with
a different approach to create the Singleton class using an inner static helper
class. The Bill Pugh Singleton implementation goes like this;
package com.journaldev.singleton;
private BillPughSingleton(){}
package com.journaldev.singleton;
import java.lang.reflect.Constructor;
When you run the above test class, you will notice that hashCode of both the
instances is not same that destroys the singleton pattern. Reflection is very
powerful and used in a lot of frameworks like Spring and Hibernate, do check
out Java Reflection Tutorial.
7. Enum Singleton
To overcome this situation with Reflection, 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.
package com.journaldev.singleton;
INSTANCE;