Preface: I've already researched why "enum inheritance" is illegal in Java.
My problem is the following: given a class Recipe, I want its property category to be a list of constant values like APPETIZER, BREAKFAST, DESSERT, MAIN_COURSE, SOUP - I logically use enums for this.
My question then is: if I wanted each of this enums to have "children of their own" (for example: SWEET and SAVORY for BREAKFAST, or CAKE, MUFFIN and BISCUITS for DESSERT), so that:
- Specifying the subcategory ("child enum") is mandatory (e.g.
myRecipe.setCategory(DESSERT)should raise an exception); - Using a "child enum" from a different "family" is forbidden (e.g.
SOUP.BISCUITSshould raise an exception); - I should be able to access the "child enum" through dot notation (e.g.
myRecipe.setCategory(DESSERT.CAKE)) - or other similar "lightweight" syntax.
I haven't been able to come up with any clean solution in Java to fit all three requisites.
Is there any "esoteric" design pattern for this? How would you implement this?