Joshua Bloch claims that
“a single-element enumeration type is the best way to implement a
singleton”
Why? I totally disagree with this statement because enumeration is data type with some type-safe constants, like NORTH
, SOUTH
, WEST
, EAST
. I think that stable singleton can be creating using class with adding some boiler plate code. Using enumeration as fully-functional object seems misleading to me. Is it really a good approach to do it?
0
This is why:
This approach is functionally equivalent to the public field approach,
except that it is more concise, provides the serialization machinery
for free, and provides an ironclad guarantee against multiple
instantiation, even in the face of sophisticated serialization or
reflection attacks. While this approach has yet to be widely adopted,
a single-element enum type is the best way to implement a singleton.
(from http://www.informit.com/articles/article.aspx?p=1216151&seqNum=3)
Have you not read this, or are you disagreeing with Bloch’s reasoning?
6