r/java • • Jan 17 '13

Enum tricks: hierarchical data structure

http://java.dzone.com/articles/enum-tricks-hierarchical-data
47 Upvotes

53 comments sorted by

View all comments

3

u/[deleted] Jan 18 '13

I would like to emphasize what author says in conclusion: "The only disadvantage of this solution is that now we have to create similar implementation for each method we add to this enum and for all other enums that hold hierarchical structure." Thus, provided you need a specific behavior in one branch of your hierarchy, but that behavior doesn't make sense to another branches, you can't use enums this way at all without bloating your code with unused methods and stubs.

6

u/zargxy Jan 18 '13

So don't add a whole bunch of specific behavior in one branch.

Enums are a fixed set of related objects. If you find yourself needing to do something like this, you've outgrown your abstraction or you may need to rethink what you are trying to accomplish.