【问题标题】:Can I get rid of this switch and enum?我可以摆脱这个开关和枚举吗?
【发布时间】:2015-07-23 14:03:18
【问题描述】:

我的印象是,通过某种方式利用多态性,这段代码可以更简洁,但我似乎找不到合适的方法。我尝试使用访问者模式,但没有成功。

具有开关的“英雄”类:

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();
    }

}

还有一些其他的东西:

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;
}

【问题讨论】:

  • 使用地图将解决问题..
  • 我不会删除enum。您可以遍历enum。使用此方法,您的Armor 类可以自行生成。

标签: java enums switch-statement


【解决方案1】:

尝试以下方法:

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

    public abstract ItemSlot getArmorSlot(Equipment equipment);
}

然后调用:

ItemSlot armorSlot = armor.getArmorType().getArmorSlot(equipment);

【讨论】:

  • 但是这种方法更干净吗?
  • @Aeailmuawe 是的,它使用多态性并确保不会忘记新的枚举常量。如果您可以重构您的类,使 ArmorSlot 不是 Equipment 的成员,您甚至可以通过在枚举中指定一个成员变量并将其设置在枚举的构造函数中来简化这一点。
  • 原谅我的无知,但我似乎无法理解“通过在枚举中指定一个成员变量并将其设置在枚举的构造函数中”的意思。
  • @Aeailmuawe 枚举支持构造函数,你可以枚举常量可以将参数传递给构造函数。但重新考虑这可能不适用于您的情况,因为 ItemSlot 是可变的并且可能不属于 ArmorType。
【解决方案2】:

设备类中的HashMap怎么样?

像这样:

public HashMap<String, ItemSlot> itemSlots = new ItemSlots HashMap<String, ItemSlot>();

然后在你的构造函数中:

itemSlots.put("mainHand ", new ItemSlot());

然后你必须定义一个这样的方法:

public ItemSlot getItemSlot(String item) {
  return itemSlots.get(item);
}

最后,你的情况会是这样的:

return equipment.getItemSlot(armor.getArmorType());

【讨论】:

  • 我会查看地图。我猜可能有一个 HashMap?
【解决方案3】:

是的,您可以摆脱开关。请记住,枚举只是静态保证的单例。所以他们可以有方法。只需像下面这样:

public enum ArmorType {
    SHIELD {
        public ItemSlot getItemSlot(Equipment e) { return e.offHand; }
    },
    // ... repeat for all other armor types
    TRINKET {
        public ItemSlot getItemSlot(Equipment e) { return e.special; }
    };

    public abstract ItemSlot getItemSlot(Equipment e);
}

那么你可以直接拨打armorType.getItemSlot(equiment);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2015-03-02
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多