Created
May 17, 2021 16:02
-
-
Save viveknaskar/fa7c831a3c6ae3a339f24c9ff75e6e5b to your computer and use it in GitHub Desktop.
Illustration of breaking Singleton pattern using Cloneable interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Using Cloneable Interface, we can create more than one instance in a Singleton class by | |
* simply using the clone() on the first object | |
*/ | |
public class SingletonCloningExample implements Cloneable { | |
private static SingletonCloningExample single_instance = null; | |
@Override | |
protected Object clone() throws CloneNotSupportedException | |
{ | |
return super.clone(); | |
} | |
/** | |
* Making the constructor as private | |
*/ | |
private SingletonCloningExample() {} | |
/** | |
* Static method to create instance of Singleton class | |
* @return single object of 'SingletonCloningExample' class | |
*/ | |
public static SingletonCloningExample getInstance() { | |
/** | |
* Ensuring only one instance is created | |
*/ | |
if (single_instance == null) | |
single_instance = new SingletonCloningExample(); | |
return single_instance; | |
} | |
public static void main(String[] args) throws CloneNotSupportedException { | |
/** | |
* Instantiating SingletonCloningExample class with variable 'objectOne' | |
*/ | |
SingletonCloningExample objectOne = SingletonCloningExample.getInstance(); | |
/** | |
* Cloning the 'objectOne' using clone() | |
*/ | |
SingletonCloningExample objectTwo = (SingletonCloningExample) objectOne.clone(); | |
/** | |
* Checking the hashCode for both the objects which would be different, | |
* meaning the objects are different | |
*/ | |
System.out.println("Hashcode of Object 1 - " + objectOne.hashCode()); | |
System.out.println("Hashcode of Object 2 - " + objectTwo.hashCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment