Java Implementation of Singleton Pattern


Singleton Patterns

In this module, we are going to cover the Singleton Pattern. The Singleton Pattern is one of, if not the most, heavily used design patterns because of its simplicity to implement and the type of problem that it solves.


The concepts when choosing a singleton pattern are that it guarantees only one instance is going to be created. It also guarantees the control of a resource. Since this is  a creational design pattern, the instantiation of it is all controlled through the implementation of the pattern. Although, it doesn’t have to be, it is usually lazily loaded. This ties nicely with it being a creational pattern.

Examples of this in Java API or commonly used frameworks are the Runtime Environment, Logger, but depending upon the implementation this could be Factory instead of Singleton, and we shall discuss this in more detail later. Sprin Beans, if you have used the Spring Framework at all, you will quickly learn that all Spring Beans are by default Singletons. The fourth example can be Graphics Manager. When you are using a Graphics API of any kind you are going to get an instance of your graphical environment, and we only want only a single instance of those at a time.

Design – Considerations 

The singleton is responsible for creating itself and managing its life-cycle. It is static in nature, although it is not implemented using a static class typically.

The reason for not using a static class is that it needs to be thread safe, and static doesn’t necessarily guarantees this for us. There is a private instance of a singleton, hence the minus sign  or hyphen in the UML. There is also a private constructor that is marked the same way. This is because we want the singleton itself to call the constructor and nobody else. There are no parameters, and if you require parameters that is typically a  Factory Pattern and violates the rules of a singleton. 

Example – Runtime

Exercise – Create a Singleton

In this demo, we are going to create a singleton and then we are going to demonstrate that only one instance of our object will be instantiated. Later, we are going to convert it to be lazily loaded and finally, make it thread-safe singleton.

The implementation of the above class is:

Let’s code the same class so that there is lazy loading.

Let’s make it proper use for creating a database connection and create a table using that connection to see performance enhancement and resource control as there will be a single instance which connects to the database.

and it’s revised implementation class would be like:

All the codes are written in Netbeans IDE using MySQL Connector/J. I did not explain any of the SQL statements assuming that the one who is referring Design Patterns must have basic idea of Core Java.


Would You Mind Sharing?


1 Trackback / Pingback

  1. Design Patterns in Java : Creational – Never Stop Learning

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.