Design Pattern (Part 01) — Singleton Pattern

Arun prashanth
3 min readMay 23, 2021

--

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under a creational pattern as this pattern provides one of the best ways to create an object. Singleton object means creating only one object for the entire application. (per JVM). and this object is globally accessible within the application.

Intent

This pattern involves a single class that is responsible to create an object while making sure that only a single object gets created. This class provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.

Problem

The application needs one, and only one, an instance of an object. Additionally, lazy initialization and global access are necessary.

Discussion

Make the class of the single instance object responsible for the creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.

The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.

Benefits

  • Controlled access to a single instance
  • Reduced number of objects
  • Allows refinement of operations and representations

Drawback

  • Considered as anti-pattern by many
  • Misused to situations where a single instance of a class might not be required

Implementations

There are several ways a singleton design pattern can be implemented. Each of these has its own benefits and limitations. We can implement a Singleton design pattern through the following ways:-

  • Eager Initialization
  • Eager Initialization with static block
  • Lazy Initialization
  • Thread Safe Lazy Initialization
  • Double Check Lazy Initialization
  • Bill Pugh Implementation
  • Singleton with Single Instance Enum

Structure

Make the class of the single instance responsible for access and “initialization on first use”. The single instance is a private static attribute. The accessor function is a public static method.

Let’s create the basic application by using the singleton pattern.

Checklist

  1. Define a private static attribute in the "single instance" class.
  2. Define a public static accessor function in the class.
  3. Do “lazy initialization” (creation on first use) in the accessor function.
  4. Define all constructors to be protected or private.
  5. Clients may only use the accessor function to manipulate the Singleton.

Ex:
Manager class —

Application Class(Main class) —

Output —

From the above output it’s clear to us that even though we have created two separate objects, the “Manager” class only gave us the same instance.

If you see this example we create the Manager instance, first, we create a initialize class from the “Manager” and make it private and volatile. what it does to the objects is, it won't be allowed access or modification from the outside of the class. and state remained as same even in the multiple threads.

And the constructor [Manager()] of the class is set to private. because to avoid being creating objects. to make sure the object initialization is protected even via the reflection API extra checking should be done inside the constructor whether the object is initialized or not.

--

--

Arun prashanth
Arun prashanth

Written by Arun prashanth

Software Engineer at Virtusa (Pvt) Ltd.

No responses yet