Re: enum singleton
Thufir wrote:
would this be better done as an enum singleton?
"Better" in what sense? It certainly can be done using enums,
although an enum for a factory strikes me as a tad weird.
Mark Space <marksp...@sbc.global.net> wrote:
I'd say "no." I tried using enums as classes in a little personal
project, and I didn't like the result. You can't add abstract classes
to different types of enums, you can't subclass an enum type (often
desirable) and in general enums are just fiddly to work with. Putting
an interface on several enum types is not a substitute for having a
first-class class to work with.
Just like Roedy's question a few weeks back about reading/writing enums
to/from a file as strings (I think that was it), it's almost always
easier, and better, to roll your own enum-like type and not have to put
up with the inherent restrictions the language places on enums.
While I don't think an enum is a natural for a factory, I disagree
with virtually every aspect of your reasoning here, Mark Space.
In the first place, enums *are* classes. If you're using an enum, you
are perforce using it "as" a class.
I don't know what you mean by "adding" an abstract class to a type.
Subclassing an enum is not "often" desirable. The 'enum' construct is
specifically designed for a particular scenario where subclassing is
not wanted, just like the for-each construct is designed for a
scenario where knowing the index into the iterable is not wanted.
Enums are no more "fiddly to work with" than anything else, and a lot
less "fiddly to work with" than, say, Swing or threads.
I don't know what you mean by "putting an interface on several enum
types", or what you mean by a "first-class class". In fact, "first-
class class" is begging the question; by labeling non-enum classes
"first class" you are using your conclusion to support the notion that
something is wrong with enums.
It is not "almost always easier", nor "better, to roll your own enum-
like type"; /au contraire/ it is almost always easier and better to
use enums where you need a compile-time enumeration. Those "inherent
restrictions" of which you speak cover by far the largest number of
use cases for such things.
In fact you can subtype an enum; you do it automatically by providing
instance-level behavior for each enum constant. You cannot
arbitrarily subtype enums because the idea is to have compile-time
certainty of the set of allowed enum values. But you can put method
overrides and other behavior into each enum instance, and let them
carry state, and do other fun things that your so-called "first-class"
classes can do.
It's just a matter of appreciating what enums are and learning what
you can do with them.
If it's 100% clear that a class must be an enum, use an enum. If you'r=
e
not sure, look hard at rolling your own.
Nice advice, should you actually spell out any criteria for how to be
"100% clear that a class must be an enum", say, that you have a fixed
set of values determinable at compile time, that you want to enforce
in a type-safe way at compile time.
If the count of that set is one, you have what I guess the OP meant by
an "enum singleton". Otherwise, I don't understand the original
question.
In short, the implementation above is fine. I'd leave it exactly like
it is.
Unless you follow Josh Bloch's advice, alluded to by John B. Matthews
upthread, that "... a single-element enum type is the best way to
implement a singleton". Singletons are rife with danger, and using an
enum implementation protects against some of that.
--
Lew