Implementation of Singleton Pattern Class in Java Introduction

Introduction

In this article we are going to describe an important concept; that is, the singleton pattern class. The Java language has several design patterns, but the most common Singleton Pattern is being used here. The Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM.

Definition of design Pattern

A design pattern is a language-independent proven solution for solving common object-oriented design problems. Why do we say that it is a proven solution? This is because each pattern describes a problem which occurs over and over again in the programming world and it describes a solution to that problem which can be used multiple times. These solutions have been time tested.

Singleton Pattern

The Singleton design pattern ensures that only one instance of a class is created; it provides a global point of access to the object and allows multiple instances in the future without affecting a singleton class's clients. For creating a singleton pattern we make the private default constructor of your class. This private default constructor prevents the direct instantiation of the object by other classes.

Singleton_Implementation.jpg

To ensure that only one instance of a class is created we make a Singleton Pattern static. The getInstance() method returns a single instance of the class.A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.

The Following steps are for making a Singleton Pattern

Step-1 Create default constructor private.

class SingletonDemo {

 

      private static SingletonDemo singletonObject;

      /** A private Constructor prevents any other class from instantiating. */

      private SingletonDemo() {

            //    Optional Code

      }         

    }

Step-2 Create a static method which returns the instance of the singleton class.

class SingletonDemo {

 

      private static SingletonDemo singletonObject;

      /** A private Constructor prevents any other class from instantiating. */

      private SingletonDemo() {

            //    Optional Code

      }

      public static synchronized SingletonDemo getInstanceMethod() {

            if (singletonObject == null) {

                  singletonObject = new SingletonDemo();

            }

            return singletonObject;

     }

}

Step-3 Make access method synchronized for thread safe.

public static synchronized SingletonDemo getInstanceMethod() {

            if (singletonObject == null) {

                  singletonObject = new SingletonDemo();

            }

            return singletonObject;

      }

Step-4 Override the object clone method for stop cloning.

public Object clone() throws CloneNotSupportedException {

            throw new CloneNotSupportedException();

      }

}

Example

class SingletonDemo {

 

      private static SingletonDemo singletonObject;

      /** A private Constructor prevents any other class from instantiating. */

      private SingletonDemo() {

            //    Optional Code

      }

      public static synchronized SingletonDemo getInstanceMethod() {

            if (singletonObject == null) {

                  singletonObject = new SingletonDemo();

            }

            return singletonObject;

      }

      public Object clone() throws CloneNotSupportedException {

            throw new CloneNotSupportedException();

      }

}

 

public class SingletonClassDemo {

 

      public static void main(String args[]) {

            //          SingletonDemo obj = new SingletonDemo();

 

                //Compilation error not allowed

            SingletonDemo obj = SingletonDemo.getInstanceMethod();

            // Your Business Logic

            System.out.println("Singleton class is create");

            System.out.println("The output of two instance:");

 System.out.println("First Instance: "+obj.getInstanceMethod());

 System.out.println("Second Instance:"+obj.getInstanceMethod());

      }

}

 

OUTPUT

 Cmd single ton class.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all