我能摆脱这个开关和枚举吗?

Aea*_*awe 2 java enums switch-statement

我的印象是这段代码可以通过某种方式利用多态来更清晰,但我似乎无法找到一种正确的方法.我尝试使用访问者模式,但没有设法使用它.

具有开关的"Hero"类:

public class Hero {
    private Equipment equipment = new Equipment();
    // other fields

    public void equipArmor(Armor armor) {
        findCorrespondingArmorSlot(armor).equipItem(armor);
    }

    private ItemSlot findCorrespondingArmorSlot(Armor armor) {
        switch (armor.getArmorType()) {
        case SHIELD:
            return equipment.offHand;
        case BODY:
            return equipment.body;
        case HEAD:
            return equipment.head;
        case GLOVES:
            return equipment.hands;
        case BOOTS:
            return equipment.feet;
        case BELT:
            return equipment.waist;
        case AMULET:
            return equipment.neck;
        case RING:
            return equipment.finger;
        case TRINKET:
            return equipment.special;
        }
        throw new NullPointerException();
    }

    public Equipment getEquipment() {
        return equipment;
    }

    // other methods

    public class Equipment {
        public ItemSlot mainHand = new ItemSlot();
        public ItemSlot offHand = new ItemSlot();
        public ItemSlot body = new ItemSlot();
        public ItemSlot head = new ItemSlot();
        public ItemSlot hands = new ItemSlot();
        public ItemSlot feet = new ItemSlot();
        public ItemSlot waist = new ItemSlot();
        public ItemSlot neck = new ItemSlot();
        public ItemSlot finger = new ItemSlot();
        public ItemSlot special = new ItemSlot();
    }

}
Run Code Online (Sandbox Code Playgroud)

还有一些其他的东西:

public class ItemSlot {
    private static final Miscellaneous EMPTY = new Miscellaneous();

    private Item item = EMPTY;

    public Item getItem() {
        return item;
    }

    public void equipItem(Item item) {
        unequipItem();
        this.item = item;
    }

    public void unequipItem() {
        if (!isEmpty()) {
            item.addToInventory();
            item = EMPTY;
        }
    }

    public boolean isEmpty() {
        return (item == EMPTY);
    }
}

public abstract class Item {
    // fields

    public void addToInventory() {
        // code
    }

    // other methods
}

public class Miscellaneous extends Item{}

public class Armor extends Item {
    private ArmorType type;

    public ArmorType getArmorType() {
    return type;
    }

    //other methods
}

public enum ArmorType
{
    SHIELD, BODY, HEAD, GLOVES, BOOTS, AMULET, RING, BELT, TRINKET;
}
Run Code Online (Sandbox Code Playgroud)

Puc*_*uce 6

请尝试以下方法:

public enum ArmorType
{
    SHIELD(){
        public ItemSlot getArmorSlot(Equipment equipment){
            return equipment.offHand;
        } 
    },
    ...

    public abstract ItemSlot getArmorSlot(Equipment equipment);
}
Run Code Online (Sandbox Code Playgroud)

然后打电话:

ItemSlot armorSlot = armor.getArmorType().getArmorSlot(equipment);
Run Code Online (Sandbox Code Playgroud)