Use of Nested enums appropriate in this case?

design-patterns, enums, java

Solution

If that definition is constant (i.e. You know which sub types can contain every type) You can use here enum definitions as follows

enum ChartSubTypes{
    PercentArea, StackedArea, ChartSubType3;
}

enum ChartTypes{
    AreaChart(ChartSubTypes.PercentArea, ChartSubTypes.StackedArea), 
    CharType2(ChartSubTypes.PercentArea, ChartSubTypes.ChartSubType3);

    private List<ChartSubTypes> subTypes = new ArrayList<ChartSubTypes>();

    private ChartTypes(ChartSubTypes ...chartSubTypes){
        for(ChartSubTypes subType : chartSubTypes){
            subTypes.add(subType);
        }
    }

    public List<ChartSubTypes> getSubTypes(){
        return Collections.unmodifiableList(subTypes);
    }
   }

Problem

I have a requirement to support a number of `ChartTypes`. Each of these chart types can support a number of `ChartSubTypes`. For example `AreaChart` type can have `PercentArea`, `StackedArea` etc. I am thinking of using an Enum both for `ChartTypes` and `SubTypes` and then maintain a map somewhere which will be something like : ``` Map<ChartType,List<ChartSubTypes> mapTypes; ``` Can I somehow use a nested enum pattern here? If yes then how?

Original source