Singleton Design Pattern in Java

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 Pattern 
 
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.
 

Singleton Pattern in Java

 
Step 1 - Create default constructor private.
  1. class SingletonDemo {  
  2.     private static SingletonDemo singletonObject;  
  3.     /** A private Constructor prevents any other class from instantiating. */  
  4.     private SingletonDemo() {  
  5.         //    Optional Code  
  6.     }  
  7. }  
Step 2 - Create a static method which returns the instance of the singleton class.
  1. class SingletonDemo {  
  2.     private static SingletonDemo singletonObject;  
  3.     /** A private Constructor prevents any other class from instantiating. */  
  4.     private SingletonDemo() {  
  5.         //    Optional Code  
  6.     }  
  7.     public static synchronized SingletonDemo getInstanceMethod() {  
  8.         if (singletonObject == null) {  
  9.             singletonObject = new SingletonDemo();  
  10.         }  
  11.         return singletonObject;  
  12.     }  
  13. }  
Step 3 - Make access method synchronized for thread safe.
  1. public static synchronized SingletonDemo getInstanceMethod() {  
  2.     if (singletonObject == null) {  
  3.         singletonObject = new SingletonDemo();  
  4.     }  
  5.     return singletonObject;  
  6. }  
Step 4 Override the object clone method for stop cloning.
  1. public Object clone() throws CloneNotSupportedException {  
  2.     throw new CloneNotSupportedException();  
  3. }  
Example
  1. class SingletonDemo {  
  2.     private static SingletonDemo singletonObject;  
  3.     /** A private Constructor prevents any other class from instantiating. */  
  4.     private SingletonDemo() {  
  5.         //    Optional Code  
  6.     }  
  7.     public static synchronized SingletonDemo getInstanceMethod() {  
  8.         if (singletonObject == null) {  
  9.             singletonObject = new SingletonDemo();  
  10.         }  
  11.         return singletonObject;  
  12.     }  
  13.     public Object clone() throws CloneNotSupportedException {  
  14.         throw new CloneNotSupportedException();  
  15.     }  
  16. }  
  17. public class SingletonClassDemo {  
  18.     public static void main(String args[]) {  
  19.         //  SingletonDemo obj = new SingletonDemo();  
  20.         //Compilation error not allowed  
  21.         SingletonDemo obj = SingletonDemo.getInstanceMethod();  
  22.         // Your Business Logic  
  23.         System.out.println("Singleton class is create");  
  24.         System.out.println("The output of two instance:");  
  25.         System.out.println("First Instance: " + obj.getInstanceMethod());  
  26.         System.out.println("Second Instance:" + obj.getInstanceMethod());  
  27.     }  
  28. }  
OUTPUT
 
Singleton Pattern in Java


Similar Articles