Singleton Design Pattern in java

By | July 4, 2020

In the previous post, Java Design Pattern which talks list of design patterns available in java

In this post, we will learn what is Singleton design pattern and it’s different implementations. Singleton design patterns help us to create one and only one object of a class within the JVM. 

  1. Singleton Eager initialization
  2. Singleton Lazy Initialization
  3. Thread Safe Singleton
  4. Static block initialization
  5. Bill Pugh Singleton Implementation
  6. Using Reflection to destroy Singleton Pattern
  7. Serialization and Singleton
  8. Enum Singleton

 Example within JDK: 

A. Singleton Eager initialization

Singleton Class using Eager initialization

 Output of ClientTest.java

2018699554
2018699554

NOTE: Calling getInstance() method many times  returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

B. Singleton Lazy Initialization

Client Program:

Output of ClientTest.java

2018699554
2018699554

NOTE: Calling getInstance() method many times  returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

C. Thread Safe Singleton

Singleton.java

MyThread.java

ClientTest.java

Output of this client program:

pool-1-thread-1 2047457294
pool-1-thread-4 2047457294
pool-1-thread-2 2047457294
pool-1-thread-3 2047457294
pool-1-thread-5 2047457294
pool-1-thread-1 2047457294 

NOTE: Calling getInstance() method many times  by different threads returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

D. Static block initialization

Client Program:

Output of this program:

2018699554
2018699554

NOTE: Calling getInstance() method many times  returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

E.Bill Pugh Singleton Implementation

Singleton.java

ClientTest.java

Output of this client program:

2018699554
2018699554

NOTE: Calling getInstance() method many times  returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

F.Using Reflection to destroy Singleton Pattern

Singleton.java

ClientTest.java

Output of this client program:

2018699554
1311053135

NOTE: Calling getInstance() method many times returns different hashcodes that proves that the Singleton is broken by using reflection API.

G.Serialization and Singleton

Singleton.java

ClientTest.java

Output of this client program:

1028566121
1028566121

NOTE: Calling getInstance() method many times  returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

H.Enum Singleton

Singleton.java

ClientTest.java

Output of this client program:

2018699554
2018699554
Singleton!!

NOTE: Calling getInstance() method many times  returns the same hashcode that proves that the Singleton class creates only one instance within the JVM

You May Also Like:

Java Design Patterns

That’s all about Singleton Design Pattern in java
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *