Type of the pattern:

The Singleton design pattern is a creational design pattern. (Design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation, hiding the complexity of the object creation).

Problem it solves:

The singleton pattern ensures a class has only instance, and provides a global point of access to it.

Examples:

Let’s understand the scenarios where we need only one instance of a class in a JVM

  1. To avoid overuse of resources:

There should be only one instance of DB ThreadPool serving all clients’ requests as creating a new DB connection is resource intensive operation. So creating multiple instances of thread pool might lead to a fatal system error.

  1. To avoid incorrect program behavior or inconsistent results:

When same state of an object is processed by many other types of objects then it should be a singleton object so that same information is shared across all the objects.  When the object state changes every other type of object gets same new state so there are no inconsistencies in the result.

Let’s see how we can have only one instance of an Object in JVM:

Method 1:

public class Singleton{

private volatile static Singleton singleton;

private Singleton(){

}

public static Singleton getInstance(){

if(null == singleton){

synchronized(Singleton.class){

if(null == singleton){

singleton  = new Singleton();

}

}

}

return singleton;

}

 

How this code makes sure that there will be one instance in JVM:

 

  1. Private constructor: private fields can only be accessed from class body. This makes sure no other class can create an instance of Singleton using new operator. It can be done only from some static method in the class. User has to call getInstance() static method to get reference of Singleton class.

 

  1. Null check in Synchronized block on class instance of Singleton: In multithreaded system if two or more threads enter getInstance() method at the same time, they might end up creating two instances of singleton. Synchronized block makes sure only one at a time checks for null state and will create an singleton instance. Second thread which is waiting for access to the monitor (synchronized block) will be stopped from creating one more instance of singleton because of double null check.
  2. Volatile modifier: It forces a thread to read the value of a variable from memory every time it needs. So Object reference assigned to the singleton variable by a thread entering first in the synchronized block will be read by all other threads waiting for the monitor when they do null check.

 

Method 2:

 

There is second and much better approach for creating singleton though. Use Enum

 

Enum Singleton{

INSTANCE;

}

Creation of Enum instance is thread safe so it solves all the issues related to thread safety in previous approach.

 

 

Singleton pattern

Post navigation


Leave a Reply

Your email address will not be published. Required fields are marked *