Fwd: Regarding objective Question doubt (Singleton cloning)

5 views
Skip to first unread message

komal deoda

unread,
Mar 16, 2018, 8:51:35 AM3/16/18
to comps...@googlegroups.com
---------- Forwarded message ----------
From: "Madhavi Sarjare" <madhavis...@gmail.com>
Date: 16-Mar-2018 16:05
Subject: Regarding objective Question doubt (Singleton cloning)
To: "komal deoda" <komal...@gmail.com>, "Simran Motwani" <simran.m...@gmail.com>
Cc:

Dear Students,
Plz refer following for the doubts raised?


Can we create a clone of a Singleton object?

True.It is possible to get a clone of Singleton object.Throw exception within the body of clone() method to prevent cloning.


Singleton Pattern says that just"define a class that has only one instance and provides a global point of access to it".

In other words, a class must ensure that only single instance should be created and single object can be used by all other classes.

There are two forms of singleton design pattern

Early Instantiation: creation of instance at load time.

Lazy Instantiation: creation of instance when required.

To implement cloning, we have to implement java.lang.Cloneable interface and override clone() method from Object class. It is a good idea to prevent cloning in a singleton class. To prevent cloning on singleton object, let us explicitly throw CloneNotSupportedException exception in clone() method.

Hence to create a clone of an object, the object must override the clone() method of the Object class. Thus our SingletonClass can only be cloned if it explicitly implements the clone() method.


There are some ways by which you can clone it, one of them being 

SingletonObject obj = SingletonObject.getSingletonObject();

SingletonObject clone = (SingletonObject) obj.clone();

A singleton object that guarantees one instance of the class, and never more than one. Right? Well.... not quite. Where there's a will, there's a way - it is still possible to evade all our defensive programming and create more than one instance of the singleton class defined above. Here's where most articles on singletons fall down, because they forget about cloning. Examine the following code snippet, which clones a singleton object.

public class Clone { public static void main(String args[]) throws Exception { // Get a singleton SingletonObject obj = SingletonObject.getSingletonObject(); // Buahahaha. Let's clone the object SingletonObject clone = (SingletonObject) obj.clone(); } }

Okay, we're cheating a little here. There  isn't a clone() method defined in SingletonObject,  but there is in the java.lang.Object class which it is inherited from. By default, the clone() method is marked as protected, but if your SingletonObject extends another class that does support cloning, it is possible to violate the design principles of the singleton.  So, to be absolutely positively 100% certain that a singleton really is a singleton, we must add a clone() method of our own, and throw a CloneNotSupportedException if anyone dares try!



Thanks n Regards,
Madhavi
Reply all
Reply to author
Forward
0 new messages