0

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?

Heisenberg
  • 167
  • 1
  • 6

1 Answers1

2

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?

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • 1
    I disagree with Bloch on this one, because enumerations purpose is NOT the same as the classes purpose - it should be used to store enumerated values not act like a fully functional class. – Heisenberg Jan 23 '15 at 09:22
  • @Heisenberg what's wrong with enumerated values encapsulating behaviour in form of methods? In a sense, a singleton _is_ actually an enumerated value. The one and only value of its type. – toniedzwiedz Jan 23 '15 at 09:27
  • I don't think it should be doing some operations on data. – Heisenberg Jan 23 '15 at 09:30
  • Here are some legit operations that I have used in enums: decoding legacy enum values (received by outdated remote clients) that I've since refactored away; providing useful coarser flags than the enum itself does ("Is this role an admin role?"); evaluation methods for an enum representing arithmetic operations; associating I18N text keys with menu options... All of these should go into the `enum` rather than an external helper class, by the principles of good factoring. Java would be a worse language if `enum`s didn't support methods. – Kilian Foth Jan 23 '15 at 09:35
  • 2
    @Heisenberg "...enumerations purpose is NOT the same as the classes purpose" It doesn't matter what you think their purpose is; that's subjective. What isn't subjective is that you can use them as singletons, all the bookkeeping is done for you automatically, and it works at least as well as a thorough and complete hand-rolled implementation. There's no good reason to try to reimplement it yourself and risk screwing it up. Besides, every enum is a class (`enum Foo` == `class Foo extends Enum`) so it's a moot point. – Doval Jan 23 '15 at 13:12
  • @Heisenberg It doesn't matter what `enum`'s original purpose was. This is something that was found afterwards: that it's the safest way to write singletons. All other ways are error-prone or bugged in subtle ways. – Andres F. Jan 23 '15 at 13:28