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.

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.

Comments

  1. You'd better call this "Two ways of misusing enums that people should forget".

    ReplyDelete
  2. I concurr with Jesper.

    ReplyDelete
  3. @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.

    I haven't seen anyone use an enum for a utility class

    ReplyDelete
  4. @Jesper + @therealfalcon, I am assuming this is a gut reaction rather than a reasoned argument. ;)

    ReplyDelete
  5. Since 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?

    If 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!

    ReplyDelete
  6. 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.

    I 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.

    ReplyDelete
  7. >I wouldn't use a singleton as a state machine.
    >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.

    ReplyDelete
  8. Peter Lawrey

    The man, who suggested to fuck up enums. Poor enums. Poor, poor enums.

    ReplyDelete
  9. @therealfalcon, I find your arguments emotive rather than based on any good technical reasoning. You appear to raise more questions than you answer.

    I 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.

    ReplyDelete
  10. 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.

    ReplyDelete
  11. I 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

Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Unusual Java: StackTrace Extends Throwable