We used have  Singleton Design Pattern in our applications whenever it is needed. As we know that in singleton design pattern we can create only one instance and can access in the whole application. But in some cases, it will break the singleton behavior.

There are mainly 3 concepts which can break singleton property of a singleton class in java. In this post, we will discuss how it can break and how to prevent those.

Here is sample Singleton class and SingletonTest class.

Singleton.Java

package demo1;

public final class Singleton {

    private static volatile Singleton instance = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

SingletonTest.java


package demo1;

public class SingletonTest {
    public static void main(String[] args) {
        Singleton object1 = Singleton.getInstance();
        Singleton object2 = Singleton.getInstance();
        System.out.println("Hashcode of Object 1 - " + object1.hashCode());
        System.out.println("Hashcode of Object 2 - " + object2.hashCode());
    }
}

Here is output, you can see it the same hashcode for objectOne and objectTwo

Hashcode of Object 1 - 1836019240
Hashcode of Object 2 - 1836019240

Now we will break this pattern. First, we will use java reflection.

Reflection

Java  Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime. Using Reflection API we can create multiple objects in singleton class. Consider the following example.

ReflectionSingleton.java

package demo1;

import java.lang.reflect.Constructor;

public class ReflectionSingleton {
    public static void main(String[] args)  {

        Singleton objOne = Singleton.getInstance();
        Singleton objTwo = null;
        try {
            Constructor constructor = Singleton.class.getDeclaredConstructor();
            constructor.setAccessible(true);
            objTwo = (Singleton) constructor.newInstance();
        } catch (Exception ex) {
            System.out.println(ex);
        }

        System.out.println("Hashcode of Object 1 - "+objOne.hashCode());
        System.out.println("Hashcode of Object 2 - "+objTwo.hashCode());

    }
}

Example to show how reflection can break the singleton pattern with Java reflect. You will get two hash code as below. It has a break on the singleton pattern.

Hashcode of Object 1 - 1836019240
Hashcode of Object 2 - 325040804

Prevent Singleton pattern from Reflection

There are many ways to prevent Singleton pattern from Reflection API, but one of the best solutions is to throw run time exception in the constructor if the instance already exists. In this, we can not able to create a second instance.

    private Singleton() {
        if( instance != null ) {
           throw new InstantiationError( "Creating of this object is not allowed." );
        }
    }

Deserialization

In serialization, we can save the object of a byte stream into a file or send over a network. Suppose if you serialize the Singleton class and then again de-serialize that object will create a new instance, hence deserialization will break the Singleton pattern.

Below code is to illustrate how the Singleton pattern breaks with deserialization.

Implements Serializable interface for Singleton Class.

DeserializationSingleton.Java

package demo1;

import java.io.*;

public class DeserializationSingleton {

    public static void main(String[] args) throws Exception {

        Singleton instanceOne = Singleton.getInstance();
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream("file.text"));
        out.writeObject(instanceOne);
        out.close();

        ObjectInput in = new ObjectInputStream(new FileInputStream("file.text"));
        Singleton instanceTwo = (Singleton) in.readObject();
        in.close();

        System.out.println("hashCode of instance 1 is - " + instanceOne.hashCode());
        System.out.println("hashCode of instance 2 is - " + instanceTwo.hashCode());
    }

}
The output is below and you can see two hashcodes.

hashCode of instance 1 is - 2125039532
hashCode of instance 2 is - 381259350

Prevent Singleton Pattern from Deserialization

To overcome this issue, we need to override readResolve() method in Singleton class and return same Singleton instance. Update Singleton.java, with below method.

   protected Object readResolve() { 
           return instance; 
     }  

Now run above DeserializationDemo class and see the output.

hashCode of instance 1 is - 2125039532
hashCode of instance 2 is - 2125039532

Cloning

Using the "clone" method we can create a copy of original object, samething if we applied clone in singleton pattern, it will create two instances one original and another one cloned object. In this case will break Singleton principle as shown in below code.

Implement the "Cloneable" interface and override the clone method in the above Singleton class.

Singleton.java


    @Override
    protected Object clone() throws CloneNotSupportedException  {
        return super.clone();
    }

Then Test with cloning for breaking the singleton
CloningSingleton.java


public class CloningSingleton {
    public static void main(String[] args) throws CloneNotSupportedException, Exception {
        Singleton instanceOne = Singleton.getInstance();
        Singleton instanceTwo = (Singleton) instanceOne.clone();
        System.out.println("hashCode of instance 1 - " + instanceOne.hashCode());
        System.out.println("hashCode of instance 2 - " + instanceTwo.hashCode());
    }

}

Here is the output

hashCode of instance 1 - 1836019240
hashCode of instance 2 - 325040804

If we see the above output, two instances have different hashcodes means these instances are not the same.


Prevent Singleton Pattern from Cloning

In the above code, breaks the Singleton principle i. e created two instances. To overcome the above issue we need to implement/override clone() method and throw an exception CloneNotSupportedException from clone method. If anyone try to create clone object of Singleton, it will throw an exception as see below code.

    @Override
    protected Object clone() throws CloneNotSupportedException  {
        throw new CloneNotSupportedException();
    }

Now we can run the CloningSingleton class, it will throw CloneNotSupportedException while creating a clone object of Singleton object.


13

View comments

  1. Well explained . Great article on singleton pattern . There is also good singleton pattern example visit Singleton class example

    ReplyDelete
  2. It is amazing to visit your site. Thanks for sharing this information, this is useful to me...

    Microservices Online Training
    Microservices Training in Hyderabad

    ReplyDelete
  3. Are you looking for where to buy hemp oil in Australia? Visit Ricks Hemp Oil store to choose from a premium organic range of hemp seed oil products today!


    cbd oil for sale australia
    what is hemp oil
    medicinal hemp oil

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Yes, Nowadays mobile application is an Important one, This blog is more informative. Thank you For sharing your knowledge About react Native App Development.

    ReplyDelete
  6. Nice information. It is very useful for me to learn and understand easily. Thanks for sharing
    react js online training

    ReplyDelete
  7. Thanks for providing this useful knowledge! In the field of mobile development, Slack clone React Native has a significant presence. And it gets better and better with each new release in terms of development speed and performance. Building a business application used to take a long time, but now that react-native is available, it's possible to construct a messaging platform app in minutes.

    ReplyDelete
  8. microservices

    Transform your legacy application with a microservices architecture that breaks your application into small components independent from each other.

    to get more - https://www.nitorinfotech.com/services/microservices/

    ReplyDelete
  9. Great blog, keep sharing such good work.
    Hire Backend Developer | Hire Developer | Hire Node JS Developer

    ReplyDelete
  10. A very informative blog! \Hire React Developers to create interactive user interfaces using JavaScript library.

    ReplyDelete
  11. Great post on ReactJS and the Virtual DOM! The explanation of how these technologies work together is very insightful. On a related note, if anyone is interested in Full-stack web development USA, I found a fantastic resource that offers comprehensive web development services. You can learn more about it here. Thanks for sharing this informative content!
    Visit https://l4rgdigitalplus.com/web-development-services-in-usa

    ReplyDelete
Prevent the breaking of a Singleton Class Pattern
Prevent the breaking of a Singleton Class Pattern
13
Design Patterns for Microservices
Design Patterns for Microservices
12
API Monetization Models
API Monetization Models
1
Kubernetes command-line tool for Windows
2
WSO2 Enterprise Integrator with message broker profile
1
Messaging Patterns on Enterprise integration
Messaging Patterns on Enterprise integration
2
Writing Micro Services with msf4j
SMPP to wso2 ESB / EI
1
WSO2 APIM - Deployment Patterns and Profiles
SMS with WSO2 ESB
5
Reading Value from uri-template in WS02 ESB
1
Estimation for Software project development
Estimation for Software project development
1
JAVA8 Stream API and New Class Optional
Lifecycle of a Book in WSO2 Greg
1
Enterprise Data integration Directions
Enterprise Data integration Directions
Handling BigDecimal in Talend
Vehicles registration services - Part 01– PayloadFactory and Validate with JSON
1
Handling simple denormalized data from Talend
WSO2 ESB with JavaScript Object Notation
Cleaning OSSIM Alarms
Syscheck in OSSEC
Syscheck in OSSEC
Triggering action or email over the event occurrence in OSSIM
Adding More user data field for Event
Connecting to OSSEC rule from OSSIM
Creating New Rule set for OSSEC Server
1
OSSEC Rule Testing
Sending Brute force attack
DiskPart in window (Fdisk in windows 8)
Uncomplicated Firewall
Uncomplicated Firewall
Grep quotes in Linux
Grep quotes in Linux
OSSEC Decoder
How access log work with OSSIM
HIDS Agentless in AlienVault USM
Install OSSIM
OSSEC configure to new log file
OSSEC configure to new log file
Testing Log forwarding in OSSEC
Host based firewall in Linux
Adding OSSEC client to OSSEC Server
Creating Correlation Rules and Alarms in AlienVault
Advance Tutorial in OSSIM Directive
Simple OSSIM Directives
Making OSSIM Alarm from Event
Reading a custom log file from OSSIM
2
OSSIM components
module.exports VS exports
Adding agent for OSSIM from OSSEC
OSSEC service for Centos7
1
JavaScript references with setTimeout() and setInterval()
JavaScript references with setTimeout() and setInterval()
CROS in Node
CROS in Node
Node.JS with Express Session
Node.JS with Express Session
dhis2-android-dashboard Build from Source
Python make life easy
Python make life easy
Installing NodeJS in CentOS
Packaging and Distributing Python Projects
Zeppelin Data Validation Service
Zeppelin Data Validation Service
Introducing New Chart Library and Types for Apache Zeppelin
1
Tutorial with Map Visualization in Apache Zeppelin
Zeppelin Docs
Data validation
Generate a AngularJS application with grunt and bower
7
Git simple Feature Branch Workflow
Git simple Feature Branch Workflow
Workflows for Git
Workflows for Git
1
Chart Types and Data Models in Google Charts
Options for Google Charts
Google Chart with AngularJS
3
Grammar induction
Adding Configuration file for Python
NLTK tutorial–03 (n-gram)
NLTK tutorial–02 (Texts as Lists of Words / Frequency words)
Natural Language Toolkit (NLTK) sample and tutorial - 01
AffinityPropagation Clustering Algorithm
AffinityPropagation Clustering Algorithm
Building Zeppelin in windows 8
1
Zeppelin Note for load data and Analyzing
Zeppelin NoteBook
Data Binding in Angular
5
AngularJS and Angular Directives
AngularJS and Angular Directives
19
Density-based clustering algorithm (DBSAN) and Implementation
scikit-learn to generate isotropic Gaussian blobs
1
CouchDB 2.0 (Developer Preview) with HTTP APIs
CouchDB-fauxton introduction
2
Building Apache Zeppelin
CouchDB with Fauxton in windows 8
Installing Flask in Windows8
1
Basic Functionality of Series or DataFrame in Pandas
Pandas for Data Manipulation and Analysis
Pandas for Data Manipulation and Analysis
Install python 2.7.X on Ubuntu
Install python 2.7.X on Ubuntu
Python For Beginners
Python with CSV
Python Class
Python Class
Maven 3.3.x for Mint
Bower: Front-end Package Manager
Predictive modeling
Predictive modeling
Gaussian function
Installing external package for canopy / (python)
Regular Expressions with Python
2
Python Code for File reading
Estimation Theory
AngularJS and solr
3
Apache Solr and Search
I am
I am
Archives
Total Pageviews
Total Pageviews
2 0 5 7 7 0 6
Categories
Categories
Loading