jeudi 27 avril 2017

Modelling class hierarchy

I'm currently modeling a class hierarchy so wanted to check if anyone has any better way of doing this. The problem is as follows:

I have a menu item that has id and icons inside it. Then I can have also menu item that has other sub-menu items (but not icons at the same time). It is basically container for the sub-menu items, but has also id as the real menu item. Current model is something like this:

class SubMenuItemTypeId{}
class MenuItemTypeId{}
class MenuIconId{}
class MenuIcon{}

interface MenuItemWithoutSubItems{
    MenuItemTypeId getTypeId();
    List<MenuIcon> getIcons();
}

interface MenuItemWithSubItems{
    MenuItemTypeId getTypeId();
    Map<SubMenuItemTypeId, SubMenuItem> getTypeKeyToSubMenuItem();
}

interface SubMenuItem{
    SubMenuItemTypeId getTypeId();
    List<MenuIcon> getIcons();
}

interface Publishable{
    void suspend();
    void publish();
}

interface ControllableMenuItemWithoutSubItems extends MenuItemWithoutSubItems, Publishable{
    void control(MenuIconId iconId, String iconData);
}

interface ControllableMenuItemWithSubItems extends MenuItemWithSubItems, Publishable{
    void control(SubMenuItemTypeId typeid, MenuIconId iconId, String iconData);
}

The problem - it seems like I can make it more generic. Only thing that came to my mind was this variation:

enum EnumSubMenuItemTypeId implements SubMenuItemTypeId{
    ONLY_IF_MENU_ITEM_WITHOUT_SUB_ITEMS(0);
    private int id;
    EnumSubMenuItemTypeId(int id){
        this.id = id;
    }
    @Override
    public Integer getId() {
        return id;
    }
}

class RealSubMenuItemTypeId implements SubMenuItemTypeId{
    private int id;
    @Override
    public Integer getId() {
        return id;
    }
}

interface SubMenuItemTypeId{
    Integer getId();
}

class MenuItemTypeId{}
class MenuIconId{}
class MenuIcon{}

interface MenuItem{
    MenuItemTypeId getTypeId();
    Map<SubMenuItemTypeId, SubMenuItem> getTypeKeyToSubMenuItem();
}

interface SubMenuItem{
    SubMenuItemTypeId getTypeId();
    List<MenuIcon> getIcons();
}

interface Publishable{
    void suspend();
    void publish();
}

interface ControllableMenuItem extends MenuItem, Publishable{
    void control(SubMenuItemTypeId typeid, MenuIconId iconId, String iconData);
}

But problem with this is that if I want MenuItemWithoutSubItems I have to make MenuItem and as SubMenuItemTypeId in Map I have to use EnumSubMenuItemTypeId.ONLY_IF_MENU_ITEM_WITHOUT_SUB_ITEMS -> which is a workaround in my case. Classes are simpler, but it still seems more complex than needs to.

Any other options that I could have?

Aucun commentaire:

Enregistrer un commentaire