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
posted
0
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
posted
0
perhaps just make the appropriate constructor private?
Thomas Taeger
Ranch Hand
Joined: Dec 16, 2002
Posts: 307
posted
0
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.