2

Why would you use an enum to create a singleton pattern? To what purpose would it serve over a conventional singleton pattern?

I have seen the above used. The code uses an enum to create this pattern without benefit. It confuses the situation as the enum is not used to store a "set of named integral constants" but simply a method.

Mark W
  • 131
  • 5
  • 1
    http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – MichelHenrich Dec 17 '14 at 12:16
  • Thanks for the duplicates, though im not asking how its used, but in what situations it would be best. I'll rephrase – Mark W Dec 17 '14 at 12:23
  • 2
    Note, that the singleton pattern itself is considered an antipattern, regardless how you implement it. (Having only one instance of a "normal" class is fine though) – valenterry Dec 17 '14 at 12:43

1 Answers1

6

You wouldn't use it. It's mainly out there as an exercise to help you understand the interesting properties of java enums, for which there are a few uses beyond the more traditional C++ style uses. It's less verbose and requires some relatively deep knowledge of the language, which makes it attractive to the "look how clever I am" breed of programmers. You don't want to be that guy.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479