wood burning stoves 2.0*
The moose likes Java in General and the fly likes clone() overriding in singleton class 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() overriding in singleton class" Watch "clone() overriding in singleton class" New topic
Author

clone() overriding in singleton class

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

a singleton class should have only one instance. It is quite possible to create a clone copy of singleton instance.which may break down the Singleton pattern.

should a singleton class override a clone() method of object class ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

If the singleton extends Object or some other class that itself isn't Cloneable you don't need to override clone(). Only if it extends a class that is Cloneable then you need to override it. Fortunately, returning this is allowed from the clone() method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

naveen yadav wrote:hi ranchers ,

a singleton class should have only one instance. It is quite possible to create a clone copy of singleton instance.which may break down the Singleton pattern.


Only if you make the Singleton class implement Cloneable, which would be kind of stupid. You can also end up with multiple instances by making the constructor public, or by having the getInstance() method create a new instance every time rather than returning the same one each time.

All of these have something important in common: They only let multiple instances exist if the author of the singleton class does something stupid. So write your singleton correctly, and the pattern won't be broken.

should a singleton class override a clone() method of object class ?


No. The only reason to override that method is if you're implementing Cloneable because you want users of your class to be able to make copies of it.

(As a side note: Singleton is a vastly overused and much abused pattern, to the point where it's a good idea to make some effort to avoid it where possible.)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:If the singleton extends Object or some other class that itself isn't Cloneable you don't need to override clone(). Only if it extends a class that is Cloneable then you need to override it. Fortunately, returning this is allowed from the clone() method.


Good point.

Also allowed is throwing CloneNotSupportedException (if the class you're extending declares to throw it).

Still and all, extending a singleton from a Cloneable class seems questionable at best, but realistically more like just plain icky bad.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

which classes in java api are singleton ? i would like to see how they have implemented a singleton pattern.

can anyone name a few.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

I can't directly think of any, but the class java.util.logging.Logger is instance-limited, it uses similar principles.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

naveen yadav wrote:which classes in java api are singleton ? i would like to see how they have implemented a singleton pattern.

can anyone name a few.


Just google for java singleton example. There's no need to assume the implementations in the core API are special or superior to other examples you'll find. The canonical approach is simply:



Since 1.5, there has emerged an enum-as-singleton pattern. I don't particularly care for it, but you can easily find examples of that on the web as well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
If the superclass implements Cloneable and doesn’t declare CloneNotSupportedException, then it is not intended to be a singleton. In which case, isn’t it a breach of the Liskov Substitution Principle to try to make its subclass into a singleton?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:If the superclass implements Cloneable and doesn’t declare CloneNotSupportedException, then it is not intended to be a singleton. In which case, isn’t it a breach of the Liskov Substitution Principle to try to make its subclass into a singleton?


Yes.

A Cloneable class, by definition, is intended to be able to have multiple copies. A singleton, by definition, is intended NOT to be able to have multiple copies.



Personally, I don't think we even need to dust off LSP to notice the design smell here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone() overriding in singleton class
 
Similar Threads
problem in clone() while making singleton
Clone with SIngleton
Singleton Question
Cloning a Singleton Object
Singleton class + object.clone() ?