• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clone() overriding in singleton class

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which classes in java api are singleton ? i would like to see how they have implemented a singleton pattern.

can anyone name a few.
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't directly think of any, but the class java.util.logging.Logger is instance-limited, it uses similar principles.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic