Why use a enum to create the singleton pattern [duplicate]
https://softwareengineering.stackexchange.com/questions/266677
-
06-10-2020 - |
Question
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.
Solution
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.