aspose file tools*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes singleton object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "singleton object " Watch "singleton object " New topic
Author

singleton object

Consta Chertakovsky
Greenhorn

Joined: Feb 23, 2006
Posts: 4
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


enjoy
John Waugh
Greenhorn

Joined: May 01, 2006
Posts: 6
For Singleton check can also be added in constructor to throw exception if object is already there for that class.
don brown (aka brownie brownie)
Ranch Hand

Joined: Jan 16, 2006
Posts: 30
perhaps just make the appropriate constructor private?
Thomas Taeger
Ranch Hand

Joined: Dec 16, 2002
Posts: 307
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


www.classic-and-class.com - www.evalulearn.com
Interfaces are the glue of OO.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton object