Two uses for enum most people forget.
The enum can be used to create enumerated values and even many instances with different implementations but there are two simple uses which are appear to be under utilised.
A utility class is a class with no instances. To declare this as an enum is trivial. You just give it no instances.
A singleton which is a class which has one and only one instances, ideally loaded lazily. Using the lazy loading of classes can give the same benifit in a simpler/thread safe way. To declare a singleton as an enum, you give it one instance and only reference the instance rather than the class.
An enum can implement an interface, allowing it to be mocked out where ever the interface has been used.
The utility class
A utility class is a class with no instances. To declare this as an enum is trivial. You just give it no instances.
public enum MyUtils {;
public static String process(String text) { /* ... */ }
}
The singleton
A singleton which is a class which has one and only one instances, ideally loaded lazily. Using the lazy loading of classes can give the same benifit in a simpler/thread safe way. To declare a singleton as an enum, you give it one instance and only reference the instance rather than the class.
public enum Singleton implements SingletonService {
INSTANCE;
public String instanceMethod(String text) { /* ... */ }
}
An enum can implement an interface, allowing it to be mocked out where ever the interface has been used.
You'd better call this "Two ways of misusing enums that people should forget".
ReplyDeleteI concurr with Jesper.
ReplyDelete@Jesper, Using an enum for a singleton is becoming more common, at least on the stack overflow forum. I would say it could replacing some of the older, more complicated forms of singleton.
ReplyDeleteI haven't seen anyone use an enum for a utility class
@Jesper + @therealfalcon, I am assuming this is a gut reaction rather than a reasoned argument. ;)
ReplyDeleteSince when has a classical singleton been complicated? Since when has a DI-Singleton been complicated? Am I missing something? Even thread synchronization isn't much of an issue. I also loose most object oriented features of Java. Why should I abuse an enum?
ReplyDeleteIf I use it like a state machine it just makes the code complicated, lot of code in just one file when there really should be multiple classes. There's really no application for this!
A classical singleton is more complicated than using an enum with one instance. It is not too complicated, but I don't see the point in making something more complication than it needs to be.
ReplyDeleteI couldn't find a "DI Singleton" using google, perhaps you could explain what you mean?
No issue at all is still simpler than "not much issue"
How is having an enum with one instance an abuse? Where does it say an enum cannot have one instance?
I wouldn't use a singleton as a state machine. I don't see why using a singleton means you cannot use multiple classes.
I don't have many singletons, all of them are stateless and all of them are enums. Most have two or three methods and it wouldn't make sense to break them up into more classes.
>I wouldn't use a singleton as a state machine.
ReplyDelete>I don't see why using a singleton means you
>cannot use multiple classes.
Some people on stackoverflow suggested using overcomplicated enums instead of a state machine pattern.
BTW: DI Singleton means container managed lifetime and instantiation control (Dependency Injection Singleton). It's a quite commeon abbrevation on many forums.
The enum does have a big disadvantage. The way you use it is unnatural, unintuitive and therefor not clean. It's a pretty dirty hack for the lazy imho.
Peter Lawrey
ReplyDeleteThe man, who suggested to fuck up enums. Poor enums. Poor, poor enums.
@therealfalcon, I find your arguments emotive rather than based on any good technical reasoning. You appear to raise more questions than you answer.
ReplyDeleteI have demonstrated you can use enums for a state machine pattern. You haven't said what the difference is.
You haven't said what you believe is the disadvantage of using an enum are. In other articles where people have suggested disadvantages and I have included them in the article.
The functionality for enums I have used is outlined in Sun's Enum tutorial.
Doing things the smart and simple way could be considered lazy. Lazy is not also a dirty word in programming. ;) Too many developer insist on doing things the hard way but this is not productive IMHO.
I haven't suggested changing the way enum's work. Perhaps you could clarify what you mean?
Perhaps you have a poor impression of enums and what they can do.
I like using Enum as Singleton at least its shot and concise than Classic version of Singleton . I missed this when I wrote 10 Examples of Enum in Java , glad to know this.
ReplyDeleteI was directed to this blog entry while raising the question here: http://stackoverflow.com/questions/25137490/zero-instance-enum-vs-private-constructors-for-preventing-instantiation I can't believe people don't see the benefit of the 0-instance idiom! Controlling instantiation is precisely what enums is all about! On the other hand I shouldn't be too surprised. Many people use null to represent empty lists... Hey, an empty list is not *really* a list after all, is it? ;-)
ReplyDelete