Singleton Design Pattern

From this article I’m going to discuss about few design patterns in detail with the example codes. First let’s discuss the simplest design pattern, Singleton design pattern.

🔎 What is Singleton Design Pattern?

Singleton design pattern coming under the creational category. So it is responsible for creating only one object (per JVM) from a single class and provide global accessibility for that single object. This pattern is widely used for ;

  • logging
  • drivers objects
  • caching
  • External resources / hardware interface access
  • Configuration and
  • thread pool.

It is a based design pattern for other design patterns like Builder, Prototype, Facade, and Abstract Factory etc. This is the design pattern used in some core java classes like java.lang.Runtime and java.awt.Desktop .

Here is the class diagram of the singleton pattern. It has private instance of the particular class , private constructor of the class and has public method called getInstance and return an object of a class.

🔎 Implementation of Singleton Pattern

When implementing the singleton pattern there are several ways to do that. In the implementation you have to consider about three things.

  • Private constructor: It is used to restrict the class instantiation from another classes
  • Private static variable: It creates private static variable of same class and it is the only one instantiation of the class
  • Public static method: We need to keep the global access point for others and that is why we create public static method.

Following are different types of implementation of singleton pattern at however it achieve same goal at the end.

💻 Eager initialization

In eager instantiation it created an object of a class when it is loaded to the memory by JVM. It is suitable for a program that needs running an object always in the program.

Drawback of this implementation is it creates an instance whether it is used or not. This is suitable for the singleton class that is not used much resources. But generally singleton is used for the DB connection, file system and they are normally used high resources. Therefore waiting until the client call getInstance . Other thing is that it doesn’t support for exception handling.

💻 Static block initialization

This is also like eager initialization and different is it allows for exception handling via the static block. As the static block you don’t want to create getInstance();.

Though this support for exception handling, it is not a best practice because it waste resources and also waste CPU time. Therefore developers move for the lazy loading techniques.

💻 Lazy Initialization

It performs well in the single thread environment and instantiate the class when clients requested it. But it causes some issues in multi thread systems.

💻 Thread Safe Singleton

To make the thread safe instantiation, it uses synchronized global access method. Therefore only a single method execute at a time. Following code segment explains its implementation.

Though this provide thread safe implementation it has high cost and reduce the performance because of the synchronized method. To reduce the effects to the performance and decrease the extra overhead, developers use double checked locking principle. In that techniques synchronized block is used inside the if condition and it only check at once to ensure the single instance was created in the class.

There are few more implementation like Bill Pugh Singleton Implementation, Using Reflection, Enum Singleton, Serialization and Singleton and you can learn them here.

💻 Singleton Use Case

Following code snippet shows the singleton pattern usage in the spelling checker class. Think you need to create a text editor application and at that time you need to run the spelling checking in everytime open the text editor. If you not run spellchecker it will throw and exception and redirect you to getInstanceOfSpellChecker();

First we implement the code without the singleton pattern and get the output.

Then it output values as following.


Now you see it generates two different hash value and it means that it creates two different object from the SpellChecker class. But we want to run same object as the SpellChecker instance.So we use singleton pattern.

Following code snippet shows the file.

Here is the output value for the hashcode value of the two instance


Now you can see it creates same object in all the time.

Stay Safe & Learn New Things!!! 😃



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thilini Weerasinghe

Thilini Weerasinghe

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in B.Sc (Hons) Computing & Information Systems. After all I am a Human...