枚举单例,由接口引用,按字符串名称实例

时间:2015-04-05 17:14:08

标签: java interface enums singleton

感谢您查看我的问题,我在搜索/图书中找不到答案。我正在通过编写roguelike来学习java,但我认为这个问题与游戏相关的问题更多的是java相关的。如果我错了,请随时教育我。

我有类似的课程,我希望每个人都有特定的能力。能力是带有一组标准方法名称的枚举单例,我会将Actor传递给 - 我想避免在每个Actor类中从接口实现方法,并且在我前进时真的很喜欢这种方法的设想用法。我来自shell / perl背景,无法判断我是不是在考虑OOP,或者我是否正在使用一些有用的东西并且还没有技能可以将它拉下来。

在StdActor中的addAbility(String)是它在这个实验中最终破坏的地方。

问题是 - 我在这里做错了吗?如果没有,我该如何实现呢?

操纵能力的界面:

public interface ActorAbility {

    // doesn't work, but need something to enable
    // instance retrieval for addAbility...
    public ActorAbility getInstance();

    public void act(Actor actor);

    public boolean isTickable();

}

清理接口的实现:

public enum ActorMove implements ActorAbility {
    INSTANCE;

    private ActorMove() {
    }

    public ActorAbility getInstance() {
        return INSTANCE;
    }

    public void act(Actor actor) {
        log.debug("Move");
    }

    public boolean isTickable() {
        return true;
    }

}

消毒使用该能力。试错了。 addAbility(String)断开,从SO和其他地方复制/粘贴。它可能需要从轨道上进行核算。

public class StdActor implements Actor {
    private HashSet<ActorAbility> abilities = new HashSet<>();

    // this whole method is wrecked
    public void addAbility(String ability) {
        // Class<? extends ActorAbility> action;  // in a maze of twisty passages...
        ActorAbility actionInstance = null;
        try {
            // action = Class.forName("game3.Actors.Abilities." + ability);
            actionInstance = ActorAbility.valueOf("game3.Actors.Abilities."
                + ability);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.abilities.add(actionInstance);
    }
}

用例:

public class StdCharClass extends StdActor {

    public StdCharClass() {
        // I like this because it's clean and easily
        // changeable
        addAbility("ActorMove");
    }
}

未来的计划用途:

HashSet<ActorAbility> abilities = actor.getAbilities();
for (ActorAbility ability : abilities) {
    if (ability.isTickable()) {
        ability.act(actor);
    }
}

谢谢!

编辑: 感谢您的快速评论,JB。我尝试了你的建议,似乎做了我所希望的。看来我刚刚离开了杂草,需要被拉回来。

1 个答案:

答案 0 :(得分:1)

新课程:

public enum Ability {
    MOVE(ActorMove.INSTANCE), FIGHT(ActorFight.INSTANCE);

    private ActorAbility ability;

    private Ability(ActorAbility abilityClass) {
        this.ability = abilityClass;
    }

    public ActorAbility getAbility() {
        return this.ability;
    }
}  

StdActor:

public class StdActor implements Actor {
private HashSet<Ability> abilities = new HashSet<>();

public void addAbility(Ability ability) {
    this.abilities.add(ability);
}

子类:

public class StdCharClass extends StdActor {

    public StdCharClass() {
        addAbility(Ability.MOVE);
    }

}

最后,用法:

HashSet<Ability> abilities = bob.getAbilities();
for (Ability ability : abilities) {
    ActorAbility abilityClass = ability.getAbility();
    if (abilityClass.isTickable()) {
        abilityClass.act(bob);
    }
}

输出!

12:44:15.835 [main] DEBUG ActorMove - Move