Singleton Design Pattern

 
Most of the times we need to create single instance of a class for ex. creating a single database connection, creating a single instance for logging or caching purpose. But how to create a single instance of a class in java ? Java provides Singleton Design Pattern to create only one instance of a class. Let’s learn in detail about this pattern.

Singleton Design Pattern is a creational design pattern. Singleton pattern is one of the simplest design patterns. This pattern restricts the instantiation of a class to one object. This is useful when exactly one object needs to be created. This pattern ensures that a class has only one instance and provides a global point of access to that instance.

Advantages of Singleton Design Pattern:

In java creating an object consumes memory. By restricting only one instance of the class creation, it saves memory as objects are not created for each request.

Rules for creating singleton object:

  • Create a private constructor to restrict the instantiation of class.
  • Create a private static variable of the same class which is the only instance of the class.
  • Create a public static method to return the single instance of the class.

 

Implementation:

Singleton design pattern can be implemented in different ways. Let’s see these one by one.

Eager Initialization:

In Eager initialization, the instance of the class is created at the time of declaring the static data member.

Let’s create MySingleton.java class.

MySingleton.java:

Here, we have created the instance of the class at the time of declaring the static data member. The constructor is private to restrict the instantiation of the class by other classes and a public static method to return the single instance of the class.

SingletonDemo.java:

Let’s create the SingletonDemo.java class.

Here we have tried to create 2 instances singleton and singleton1 of MySingleton.java class. Let’s check the output.

Result:

If you observe the output, you can see that the hash code of 2 objects are same. This means that only one object is created.

Here we have seen that the object is created at the time of class loading itself. But it is not necessary that every time we need to create an object at the time of class loading. So let’s see Singleton class implementation with lazy initialization of the object.

Lazy Initialization:

In lazy initialization, instance of a class is created when its required to be used for the first time. Let’s see the lazy initialization by an example.

Here you have seen how we can lazily instantiate an object. But what if multiple threads try to access it. The threads will get the different instances of singleton class. So we need to restrict. This can be done by simply adding synchronized keyword to the getInstance() method. Let’s see this with an example.

The above implementation of Singleton class provides thread safety, but what about the performance. As we have used synchronized keyword, it increases waiting time of thread and effects performance of the system. To avoid this issue, Double Checked Locking is used.

Double Checked Locking:

In Double Checked Locking, the synchronized block is used inside the if condition and an additional check is added to ensure that only one instance of singleton class is created.

Let’s see this with an example.

Related posts

Leave a Comment