Adding subcategories to a java Enum
<p>Suppose I have a simple Java Enum:</p>

<pre><code>public Enum itemType
{
    FRUITS("fru"),
    VEGETABLES("veg"),
    LIQUOURS("liq"),
    SODAS("sod");

    private String dbCode;

    public ItemType(String dbCode){
        this.dbCode = dbCode;
    }

    public String getDbCode(){
        return this.dbCode;
    }
}
</code></pre>

<p>I would now like to introduce a "category" to this enum, for example to make the distinction between liquid items and solid items. I found two ways of doing this within the enum class, see below. However, both suffer from the same anti-pattern: if the amount of categories or amount of items ever increases/decreases (imagine 100 item types with 10 categories!), I've got a lot of updating to do. What patterns can I use to design this enum as cleanly and re-usable as possible?</p>

<p><strong>First approach: Add additional properties to the enum</strong></p>

<pre><code>public Enum itemType
{
    FRUITS("fru",false),
    VEGETABLES("veg",false),
    LIQUOURS("liq",true),
    SODAS("sod",true);

    private String dbCode;
    private boolean liquid;

    public ItemType(String dbCode, boolean liquid){
        this.dbCode = dbCode;
        this.liquid = liquid;
    }

    public String getDbCode(){
        return this.dbCode;
    }
    public boolean isLiquid(){
        return this.liquid;
    }
}
</code></pre>

<p><strong>Second approach: Use static methods to ask about subcategories</strong></p>

<pre><code>public Enum itemType
{
    FRUITS("fru"),
    VEGETABLES("veg"),
    LIQUOURS("liq"),
    SODAS("sod");

    private String dbCode;

    public ItemType(String dbCode){
        this.dbCode = dbCode;
    }

    public String getDbCode(){
        return this.dbCode;
    }

    public static boolean isLiquid(ItemType type){
        switch(t){
            case SODA:
            case LIQOURS: return true;
            default: return false;
        }
}
</code></pre>