Home » Java Design Pattern » Singleton Design Pattern in Java

Singleton Design Pattern in Java

Singleton pattern will insure that only one instance of the class will be created by java virtual machine at any point of time. It is used to provide global point of access to the object.Singleton patterns are used in logging, caches, thread pools,getting device driver objects etc.

Scenario to use singleton Class

Singletons design pattern can be used to create a Connection Pool. If some one create a new connection object in every class that requires it, then its clear waste of resources. In this scenario by using a singleton connection class we can maintain a single connection object which can be used throughout the application.

Steps to implement Singleton Pattern

To implement this design pattern we need to consider the following four steps:

Step 1: Provide a default Private constructor

The default constructor of class should be made private, which prevents the direct instantiation of the object by Other Classes.

public class SingletonClass {

// private constructor
private SingletonClass() {

}
}

Step 2: Create a static public Method for getting the reference of the Singleton Object

This provides the global point of access to the Singleton object and returns the instance to the client calling class.

public class SingletonClass{

private static SingletonClass Object;

// Note that the constructor is private

private SingletonClass() {

}

public static SingletonClass getInstance() {

if (Object == null) {

Object = new SingletonClass();

}

return Object;

}

}

In the above class we created a public static method to get the instance of the Singleton Object. First time the object is created inside this method as it is null. Subsequent calls to this method returns the same object.

Step 3: Make the Access method Synchronized :

In multi-threaded environment it may happen that two or more threads enter the method getInstance() at the same time when Singleton instance is not created, resulting into simultaneous creation of two objects.Such problems can be avoided by defining getInstance() method synchronized.

Step 4: Override the Object clone method to prevent cloning

We can still create a copy of the Object by cloning it using the Object's clone method. Hence it is advisable to overload clone() method of Object class and throw CloneNotSupportedException exception.

public Object clone() throws CloneNotSupportedException {

throw new CloneNotSupportedException();

}

Fully Implemented Class :

public class SingletonClass {

private static SingletonClass singletonObject;

private SingletonClass() {

}

public static synchronized SingletonClass getInstance() {

if (singletonObject == null) {

singletonObject = new SingletonClass();

}

return singletonObject;

}

public Object clone() throws CloneNotSupportedException {

throw new CloneNotSupportedException();

}

}

Test Class :

public class SingletonTest {

public static void main(String args[]) {

//Compilation error not allowed
//SingletonClass obj = new SingletonClass();

SingletonClass obj = SingletonClass.getSingletonObject();

System.out.println("Singleton object obtained");

}

IMPORTANT: If you have VirtualBox installed and running, please shut down it before the installation begins.

Whats Next

You can start your Docker Journey by learning about the command you can execute in your computer terminal.

Previous Next Article

comments powered by Disqus