• 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

singleton object

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to prevent singleton object from the object cloning.
I have one solution as implement clone method with throws new UnclonableObjectexception.
If any other way we have to do it?

Apprichiate it
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Singleton check can also be added in constructor to throw exception if object is already there for that class.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps just make the appropriate constructor private?
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Consta Chertakovsky:
How to prevent singleton object from the object cloning.
... implement clone method with throws new UnclonableObjectexception.
If any other way we have to do it?


Hello Consta,

the easiest and the normal way is to leave off "implements Cloneable". Then any caller gets a similar exception as the one supposed by you.

Making any constructor private could not keep the singleton from being cloned because clone(), if Cloneable, bypasses any constructor. This bypassing any constructor is the reason for better performance.

Thomas
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic