Created
May 17, 2021 16:31
-
-
Save viveknaskar/1aca037f5d26c4e6b1d17d6eac567231 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
/** | |
* By explicitly throwing CloneNotSupported exception from the clone() | |
* would prevent object creation the second time, thereby preventing | |
* the breaking of Singleton Pattern | |
*/ | |
public class SingletonCloningPreventExample extends SuperCloneableClass { | |
private static SingletonCloningPreventExample single_instance = null; | |
/** | |
* Override the clone() to throw the same instance | |
*/ | |
@Override | |
protected Object clone() | |
{ | |
return single_instance; | |
} | |
/** | |
* Making the constructor as private | |
*/ | |
private SingletonCloningPreventExample() {} | |
/** | |
* Static method to create instance of Singleton class | |
* @return single object of 'SingletonCloningExample' class | |
*/ | |
public static SingletonCloningPreventExample getInstance() { | |
/** | |
* Ensuring only one instance is created | |
*/ | |
if (single_instance == null) | |
single_instance = new SingletonCloningPreventExample(); | |
return single_instance; | |
} | |
public static void main(String[] args) throws CloneNotSupportedException { | |
/** | |
* Instantiating SingletonCloningExample class with variable 'objectOne' | |
*/ | |
SingletonCloningPreventExample objectOne = SingletonCloningPreventExample.getInstance(); | |
/** | |
* Cloning the 'objectOne' using clone() | |
*/ | |
SingletonCloningPreventExample objectTwo = (SingletonCloningPreventExample) objectOne.clone(); | |
/** | |
* Checking the hashCode for both the objects which would be same, | |
* meaning the objects are same | |
*/ | |
System.out.println("Hashcode of Object 1 - " + objectOne.hashCode()); | |
System.out.println("Hashcode of Object 2 - " + objectTwo.hashCode()); | |
} | |
} | |
class SuperCloneableClass implements Cloneable { | |
@Override | |
protected Object clone() throws CloneNotSupportedException | |
{ | |
return super.clone(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment