This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Clone() Clonable and Singleton , how all this is related. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clone() Clonable and Singleton , how all this is related." Watch "Clone() Clonable and Singleton , how all this is related." New topic
Author

Clone() Clonable and Singleton , how all this is related.

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi All,

I am having doubt that

1) clone() --> method is used to make a copy of object
2) for that we need to implement the clonable interface for that class
and override clone() method of Object class.

3) in Singleton --> To break singleton any one can clone the object.

but why we use clone() method overriding in singleton and implement clonable interface to prevent cloning .

We need to prevent cloning in singleton and not to allow cloning
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
Prabhat Ranjan wrote:but why we use clone() method overriding in singleton and implement clonable interface to prevent cloning .

You don't. If you want to prevent cloning, do not implement Clonable. Simple.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

What i understood,

first --> clonable is used to make clone if we implement this markup interface and call clone() method on the object you want to make clone.


i am not saying you need to implement clonable. if some one has already implemented clonable in other class and you have extended/Impl the same class on that case your class would be be colnable.

so that case you need to override the clone method and throw the exception that cloneNotSupportedClasss so that if any one wanted to call clone method from your singleton class he will not break singleton.

This is my deep understanding. Clonable breaks the singleton so you need to prevent cloning by overriding the clone() method of object and throw cloneNotSupportedClass Exception.




If you are not override clone() method of object class this will surely make clone.

public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException("cloning of this class is not supported by me…");
}

above is the best example..this cleared everything.

In internet there are many stuff on the clonable but none of them has explained this way.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Prabhat Ranjan wrote: . . . if some one has already implemented clonable in other class and you have extended/Impl the same class on that case your class would be be colnable.

so that case you need to override the clone method and throw the exception that cloneNotSupportedClasss . . .
In which case you ought not to be extending the class. If you extend a Cloneable (note unusual spelling) class as not Cloneable, or override a method by throwing a new Exception, you are breaching the princple that a subclass IS‑A superclass and that an instance of the subclass IS‑AN instance of the superclass (part of the Liskov substitution principle).
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
yes you might be right. what is the correct way of doing this.

if you go through any of the detailed exmaple in internet you will find the same.

I have just shared my thought on this.

Lee me know if you are agree or not agree or any other thought around this.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Prabhat Ranjan wrote:so that case you need to override the clone method and throw the exception that cloneNotSupportedClasss so that if any one wanted to call clone method from your singleton class he will not break singleton.

Actually, you have another alternative: don't clone the object; viz:However, this just hides the point that Campbell made: You should NOT be extending a Cloneable.

I'm certainly not recommending that you do it; I simply wrote it to show you that when you're confronted by a problem, there is almost always more than one solution.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I also agree with that no need to implement Clonable interface but in some case if some one has already implemenetd Clonable interface and we have extened that class. This is special case.

What is teh difference between thye clone() method i have overided

public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException("cloning of this class is not supported by me…");
}

and you ahve added in your last email
@Override
public Single clone() throws CloneNotSupportedException {
return this;
}

both are not same.

or you want to highlight somthing outstanding over here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Prabhat Ranjan wrote:I also agree with that no need to implement Clonable interface but in some case if some one has already implemenetd Clonable interface and we have extened that class. This is special case. . . .
By “special case”, do you mean “design mistake”?

The Liskov Substitution Principle would have it that every subclass operation can be called as would a superclass operation without any surprises; if you expect the clone() method to operator without any Exceptions, then you expect the subclass’ clone() method to execute without Exceptions.
The title of this thread mentions clone() and singleton. Both of those features are iffy: cloning is iffy and singletons are iffy. But the two should be considered mutually incompatible; if you can clone something it isn’t a singleton, and vice versa.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Using return this; in a clone() method appears an obvious solution, but it does not fulfil the recommendation of clone() that x.clone() != x. Note this is a recommendation, not a requirement.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3574
    
  14

I'm part of a group that takes a more extreme view. Don't write singletons for business logic, ever. Only for tools such as logging.

Never implement Cloneable. Never extend Cloneable types. Use composition instead.

If you *really* have no option but to write a class that extends a Cloneable type, write a proper clone() method that returns a valid clone. No tricks.

If you want a class to be able to produce a copy of itself, give it a copy constructor. It almost always works better than the clone() method.

A big problem with the clone() method is that you can not clone classes with final fields. You can't let the clone's field refer to a copy of the original value.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Stephan van Hulst wrote:I'm part of a group that takes a more extreme view. Don't write singletons for business logic, ever. Only for tools such as logging.
Never implement Cloneable. Never extend Cloneable types. Use composition instead.

I'm pretty much in agreement with everything you said. Indeed, it seems to me that a Singleton that implements Cloneable, whether directly or by extension, is an oxymoron.

Winston
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
i have same question related to singleton in my new post.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
No, that is a completely unrelated question. Please stick to the topic of this thread.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
But i am not happy with recent post....and still have some confusion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
If you wish to continue the discussion in this thread, about the subject in this thread, fire away. Otherwise, read this.
 
jQuery in Action, 2nd edition
 
subject: Clone() Clonable and Singleton , how all this is related.
 
Similar Threads
some doubts in core java
Marker Interface
protected specifier for Object class
what is the use of Clonable interface?
why clone method is provided in object class and not in clonable interface