Singleton Design Pattern in Java
December 28, 2022Singleton Design Pattern in Java
Singleton Design Pattern is a creational design pattern that restricts a class instantiation to a single instance and reuse this instance when required again. This pattern is used when one single instance of an object is required across all the application like DB connection, logging mechanism, configuration settings, etc.
Singleton Design Pattern saves memory usage as the instance of the object is initialized only once and reused again when required.
Singleton design pattern describes the below algorithm to solve this case;
- Encapsulate the constructor of the class
- Implement a public static method (getInstance()) that returns the single instance of the class.
So, the class cannot be constructed outside of the class by using its constructor and getInstance() method is used to
- check whether the instance is created or not
- creates the single instance if it is not created yet and returns it
- or returns the pre-initialized instance
There are two ways to implement Singleton Design Pattern;
- Eager Instantiation: Initialize the single instance of the object during the application boot time rather than on-demand and keep it in the memory.
- Lazy Instantiation: Initialize the single instance of the object when it is required.
I implement an example Java code base in my GitHub profile in order to demonstrate these two instantiation methods. In this example, you can also find a near real-life example that is implementing how to create a JDBC connection by using Singleton Design Pattern.
You can reach this code base under my java-examples repo, named java-singleton-design-pattern.
So, let’s switch the code snippets how to implement Singleton Design Pattern in Java.
Eager Instantiation of Singleton Design Pattern in Java
Let’s see how eager instantiation works within Singleton Design Pattern.
Please check the Gist to see how I have implemented eager instantiation of Singleton Design Patter in Java.
As we have a static member in the class that initiates the constructor of the class, the instance is loaded during boot time with class loading. The constructor of the class is private so it cannot be called outside of the class and getInstance() method is used to retrieve the initialized instance of the class.
Lazy Instantiation of Singleton Design Pattern in Java
Let’s see how lazy instantiation works within Singleton Design Pattern.
Please check the Gist to see how I have implemented lazy instantiation of Singleton Design Patter in Java.
Within lazy instantiation of Singleton Design Pattern, we have;
- We have a static private attribute that guarantees that the class is loaded only once
- We have a private constructor to be sure that the class cannot be called outside of itself
- We have a public getInstance() method that returns the single instance of the class. First, it checks whether the static attribute is null or not. If null, then construct the class instance and returns it. If not null, than directly the returned the pre-initialized instance.
Now, let’s see a near real-life example. I am calling it as a near real-life example as I try to demonstrate how to create a JDBC connection with a database in Java.
In this example, JdbcConnection class guarantees that the connection is created only once and reuses that connection when required by using Singleton Design Pattern. I use the lazy instantiation method in order to implement this pattern and PostgreSQL is used as database.
Also, you can find how select, insert, delete operations are working with the defined database.
Please check the Gist to see how I have implemented this example.
In a Nutshell
Long story short, Singleton Design Pattern is a creational design pattern that is used when only a single instance of a class is required like logging, JDBC connection, configuration settings, etc.
You can reach all the code base from my GitHub profile under the java-examples repo. Also, you can find other Java tips within the same repo.
Your comments & questions are always welcomed :)