3

Our firm develops primarily in Java and supports a number of "live" systems (mainly long running server processes), all reliant on shared reference data and enumerations. Occasionally an enum definition expands to include a new value and in this case we currently redeploy all our applications when this happens, the reason being that our applications each have a private "lib" folder containing all library jars (including the "enum entity" library). Obviously this is undesirable and so I'd be interested to hear other people's recommendations or approaches.

Ideas I've thought of:

  1. Modifying each application's start script to derive the most recent "enum entity" library version and prepend the jar file to the classpath.
  2. Some kind of mechanism to dynamically class load the new enum definition at runtime.

The problem with these approaches is that applications typically have code in the format:

switch(enumVal) {
  case A:
    // Do something.
    break;
  case B:
    // Do something.
    break;
  default:
    throw new IllegalArgumentException("Invalid enum value: " + enumVal);
}

... and hence will start failing when they hit the default case. Perhaps this suggests that these entities shouldn't be enums at all; it's really a convenience trade-off in our case. Or perhaps it simply suggests that our apps are too brittle and should handle the default case more gracefully.

4
  • I think you have to rethink your app design. Remember: encapsulate what varies. Oct 28, 2011 at 10:09
  • When the enum definition expands, do all your apps start using the new value immediately? What are you using those enums for? If you add new value to an enum, but don't reference it anywhere in the application code, is it still used by the application (for example by using valueOf())?
    – socha23
    Oct 28, 2011 at 11:19
  • Enums are compile time constants. So it is not enough to restart. You must recompile all classes that use them if you change anything in the enum.
    – ssedano
    Oct 28, 2011 at 11:20
  • @Udo Fholl: Not quite true, as Java performs (late) binding at runtime and by name only. If only new elements are added to any class file('s visible interface), existing code need not be re-compiled.
    – JimmyB
    Oct 29, 2011 at 16:41

2 Answers 2

1

Mr. Smith is right: Extending enums, or the old-school "enumeration" int constants for that matter, can hardly be done right and is usually not a good approach.
As you imply, the new enumeration values require a special behavior/handling by clients using them. Once you modify the enumeration, you thus must modify the clients, too. This is really bad and you should really try to re-design it in another way.

5
  • Yes, but even enums can have methods. You are stuck with a single-level inheritance hierarchy, but they are able to have behaviour pretty much the same that a regular class. You can have different overrides for each enum constant methods also. So instead of coding that switch or if-else, move the thing to be done inside each enum/class method, and call it from the client code. Oct 28, 2011 at 11:30
  • I agree again, and may add: If the specialized behavior/handling can be implemented in a single place, in the enums themselves, so that it is the same for all clients, it should not be implemented in the clients in the first place.
    – JimmyB
    Oct 28, 2011 at 12:50
  • Oh, and note that enums do not support inheritance and, thus, no polymorphism.
    – JimmyB
    Oct 28, 2011 at 13:01
  • Enums constants are kind of subclasses of the enum "class". You can have a constant specific class body, to override a method defined in the enum for a certain enum constant. It isn't what I call a clean design, but it works. Oct 30, 2011 at 23:59
  • That's right, but clients cannot derive their own implementations of a provided enum type to change behaviour.
    – JimmyB
    Oct 31, 2011 at 9:30
1

Object orientation was invented specifically to solve this problem: your switch is really just an explicit virtual table when an implicit one would have been far better. You should consider redesigning your application to use interfaces/base classes instead of enums.

This moves the problem to object creation time, which gives the immediate advantage of a single change point for each enum. In also opens the way for the introduction of a plugin based architecture which may well not require any recompilation and may even lead to the possibility of extending your application without restarting it. Given that you speak of long running server processes, a reduction in downtime is likely to be appealing.

The easiest way to avoid recompilation is probably to adopt Dependency Injection. With a framework such as Spring you can have one or more configuration files where you specify the objects you need to create and then you can load them by name.

3
  • That's interesting. Let's imagine he had a proper OO design. Some day, new requirements are introduced and a new class (at least) must be added. What alternatives are to put it into the system without recompiling? Oct 28, 2011 at 12:15
  • Ok, so instead of a coded factory now we have an XML. But again, the new class should be introduced in the system for the DI framework to instantiate it, isn't it? How without recompiling? Just adding the class file on some folder? And another question: does the DI framework detect that the XML has been changed, or a restart is needed? Oct 28, 2011 at 12:48
  • Yes, you can add the class file to a folder or a jar, although the latter will probably prevent "hot" additions. I'm not familiar with the details of how to upgrade configuration automatically, as I never needed to do that myself. Oct 28, 2011 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.