vendredi 1 novembre 2019

Effective design to group Enum's to different conditions with many-to-many relationship

In Java, I'm using Enum and then using EnumSet to group various enum's representing similar behavior.

Something like :

enum MyConditions {
    condition_A;
    condition_B;
    condition_1;
    condition_100;
    condition_xyz;
    ... more ...

}

EnumSet<MyConditions> group1 = EnumSet.of(condition_A, condition_xyz);
EnumSet<MyConditions> group2 = EnumSet.of(condition_B, condition_xyz);
EnumSet<MyConditions> group3 = EnumSet.of(condition_xyz);
... and many more...

It feels the design is bit convulated as I've to keep making more and more EnumSet for different conditions, and the design does not support sub-grouping. For instance combining group1 & 2 to test for a parent condition.

Other proposal I've is to supply the group in the enum construction itself.

Something like :

enum MyConditions {
    condition_A(group1);
    condition_B(group2);
    condition_1;
    condition_100;
    condition_xyz(group1, group2);
    ... more ...

}

This is also not very good as this makes readibility of Enum an issue when there are multiple groups.

Any ideas on what is a good design here?

Aucun commentaire:

Enregistrer un commentaire