Site icon Automation Dojos

How To Create Singleton Class in Java

Table of Contents

Design patterns Overview

Design patterns are basically solutions to programming problems that developers normally encounter during software development. Design patterns were first documented by four authors known as the Gang of Four (GoF).

Design patterns are broadly classified into creational, structural, and behavioral patterns. Creational patterns aid in object creation, structural patterns help to alter the structure of an object while behavioral patterns mostly have to do with algorithms.

Introduction to Singleton Pattern

Singleton is a creational design pattern; it helps in creating an object of a class. The Singleton pattern ensures that there is only one instance of a class. It is generally used for a class that consumes a lot of memory or one that uses expensive resources. Singleton ensures that there is only one instance of the class, thus saving on memory and other resources. The singleton pattern is mostly used for database connections, caching, logging, and other programming situations that mandate having a single object of a class.

Singleton Pattern Implementation

There are three main ways in which the singleton pattern can be implemented. All the approaches make use of a static instance, a private constructor, and a public getter method.

Eager Initialization

In this approach, the Singleton instance is created at the time the class is loaded. The following code demonstrates creating a Singleton class using this approach:

class MySingleton {
   private static MySingleton mySingleton = new MySingleton();
   private MySingleton() {
   }
   public static MySingleton getMySingleton() {
      return mySingleton;
   }
}

This code specifies a class called MySingleton. It has a private static instance field called mySingleton that is initialized at the time of creation. It also has a private constructor. Finally, there is a public getter method called getMySingleton(). This method returns the mySingleton instance. 

So, in this approach, the singleton instance is created much before it is used. If the singleton is a heavy class using a lot of resources, then this approach is not appropriate.

Lazy Initialization

In this approach, the Singleton instance is created when it is first requested. The following code demonstrates this:

public class MySingleton {
private static MySingleton mySingleton;
   private MySingleton() {
   }
   public static MySingleton getMySingleton() {
      if (mySingleton == null) {
         mySingleton = new MySingleton();
      }
      return mySingleton;
   }
}

This approach is similar to the eager initialization approach in that it has a private static instance field, a private constructor and a public getter method. The only difference is that the singleton instance is not created at start-up. Instead, the getMySingleton() method checks if an instance of the singleton class exists. If so, it just returns it. If an instance does not exist, it creates it via the private constructor.

So, in this approach the singleton instance is created when it is first requested. This is the preferred approach for singleton classes that use a lot of resources.

Static block Initialization

In this approach, the singleton instance is created within a static block. The following code demonstrates this:

class MySingleton {
   private static MySingleton mySingleton;
   private MySingleton() {
   }
   
   static {
      mySingleton = new MySingleton();
   }
   public static MySingleton getMySingleton() {
      return mySingleton;
   }
   
}

Just like the previous approaches, this code defines a private static instance, a private constructor and a public getter method. In addition, this code also defines a static block. The singleton instance mySingleton is created in this static block. Since a static block is executed only once this ensures that there is only one instance of the singleton. Since the static block is executed when the class is loaded, the singleton instance is created at start-up just like it is done in the eager initialization approach.

The advantage of this approach over the eager initialization approach is that, exception handling code can be added in the static block. So, if there is an error while creating the singleton instance, an appropriate exception can be thrown.

Advantages of Singleton

There are several advantages of using a singleton class.  It helps it saving on memory and other resources by ensuring that there is only one instance of the class. It provides global access to the singleton instance. It gives a class control over the instantiation process.

Disadvantages of Singleton

The Singleton pattern has some disadvantages too. For one, singleton objects are often used as global variables which is against object-oriented principles like encapsulation. Secondly, singletons make unit testing difficult. A class that uses a singleton object cannot be tested by itself.

Conclusion

So, in this article, we understood the basics of the singleton design pattern. We also saw some of the ways in which you can implement the singleton pattern in Java. Finally, we understood some of the advantages and disadvantages of the singleton pattern.

Exit mobile version