Can you make use of polymorphism instead? In which way should the constructors work differently?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by u johansson: Why, Ilja Preuss, do you recommend the Visitor pattern in this case?
A java enum really is a class hierarchy, as Satou showed. So we can make use of polymorphism.
If we don't want to put that specific methods onto the enums themselves, we are in a position where we want to implement a polymorphic operation for a class hierarchy, without touching the hierarchy itself. That's exactly what Visitor is for.
Of course Java 5 (we are not yet using that) allows us to use the switch statement instead, but that one (as far as I know) won't give us a compile time error when we add a new enum and forget to add the case to the switch. (The Visitor pattern would give us a compile time error, because the visitor implementation needed to implement one more method.)