A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James:
This question came up recently. I wonder if it's something in a text book or class somewhere.
The singleton pattern usually means you want to restrict a class to exactly one instance. If the design seriously requires one instance you'd want to prohibit clone, maybe even override the method to throw an exception.
The mechanism for singleton is not specified in the pattern. The private constructor with static factory method solution is common, but not required. You can find another way to restrict creation and still allow clone if you really have to.
[ September 29, 2005: Message edited by: Stan James ]
Originally posted by Stan James:
This question came up recently. I wonder if it's something in a text book or class somewhere.
The singleton pattern usually means you want to restrict a class to exactly one instance. If the design seriously requires one instance you'd want to prohibit clone, maybe even override the method to throw an exception.
The mechanism for singleton is not specified in the pattern. The private constructor with static factory method solution is common, but not required. You can find another way to restrict creation and still allow clone if you really have to.
[ September 29, 2005: Message edited by: Stan James ]
Originally posted by Layne Lund:
clone() is restricted to protected access by default. I guess this isn't a completely solution however, since someone can extend your Singleton class and override clone(). Also other classes in the same package will be able to call clone(). It seems like it would be safest to override it with private access so it cannot be called by other classes. Of course, then someone might try to use the Reflection API. Throwing an exception seems like a decent solution to avoid this problem.
Layne
Originally posted by Mr. C Lamont Gilbert:
you cant override to be more restrictive, only more permissive.
Tony Morris
Java Q&A (FAQ, Trivia)
You could create more than one instance of a singleton using Reflection I think. But then a developer would have to purposefully ignore the fact this class is a singleton and presumably is documented as such. Used normally though you can guarentee there will only be one instance of this class per JVM.
Originally posted by Ernest Friedman-Hill:
Before you say this is impossible, have a look at the "setAccessible()" method that Constructor, Method, and Field inherit from AccessibleObject.
Tony Morris
Java Q&A (FAQ, Trivia)
Constructor is private (by Singleton definition)
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I love to throw a wrench in the works now and then ... says who?
Tony Morris
Java Q&A (FAQ, Trivia)
Tony Morris
Java Q&A (FAQ, Trivia)
Before you say this is impossible, have a look at the "setAccessible()" method that Constructor, Method, and Field inherit from AccessibleObject.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Tony Morris:
Better still, why use Object.clone()? It's of a poor design that is easily duplicated more correctly.
Originally posted by Mr. C Lamont Gilbert:
What do you mean?
Tony Morris
Java Q&A (FAQ, Trivia)
Remember to always leap before you look. But always take the time to smell the tiny ads:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
|