This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
is there any possibilities to apply clone methode on singleton class oblect?
if possible , how it will be satisfied the singleton design pattern?
if not possible, why ? and what happens by the jvm?
Of course it is possible. If your Singleton class implememnts Cloneable, and makes the clone method public, then you will be able to clone the Singleton. This may even be desirable, depending on exactly [i]what[\i] the nature of the singleton is.
What happens is what you'd expect: you get a new object with the same data inside, exactly like any other clone.
Does it satisfy the Singleton pattern? Who cares - it's not like you get points for how closely your solution conforms to a pattern. The real question is: does it solve whatever it is you are trying to do?
Strictly speaking, of course, it does _not_ count as a Singleton any more, because you've created a way to make multiple instances. However, like I said - who cares?
Why would you want to implement a Clonable Singleton? The purpose of the Singleton pattern is to ensure there is only one instance of an object in a JVM. If you want more than one, you don't want to use a Singleton.
I'm pretty sure the original poster just sees the "clone()" method in Object and is worried that someone might call it on his Singleton object.
Although every class does inherit clone(), the clone() method actually doesn't work unless (1) The class implements the Cloneable interface, and (2) The class overrides clone() to make it public. It's a very strange design!
In any case, you don't need to worry: if a class doesn't want to be cloned, it can't be. clone() only works on classes that take the specific steps above.
Originally posted by Mr. C Lamont Gilbert: .. a terrible idea... something wrong with clone()...
So why is overriding it to make it public a terrible idea (given that that's what Gosling says to do in "the Java programming language)? And given that something is terribly wrong with clone()[*], what's wrong with spreading the word about that?
--- [*]This part is just my opinion -- that Object.clone() is a crazy design that should never have been used.