简体   繁体   English

从 HashMap (java) 获取具有特定类的特定对象

[英]Get specific object with specific class from HashMap (java)

I have a pack of bean class and rule class.我有一包豆类和规则类。

I'm trying right now to create a HashMap of my differents rules to have access of those different rules in other classes.我现在正在尝试创建我的不同规则的 HashMap 以访问其他类中的这些不同规则。

In TripleTriad.java (the class that manage my game) I have:在 TripleTriad.java(管理我的游戏的类)中,我有:

public static Map<RuleType, Rule> ruleList = new HashMap<>();

private void createRuleMap(Map<RuleType, Rule> ruleList) {
    ruleList.put(COMBO, new RuleCombo());
    ruleList.put(ELEMENTALE, new RuleElementales());
    ruleList.put(IDENTIQUE, new RuleIdentique());
    ruleList.put(MEMEMUR, new RuleMemeMur());
    ruleList.put(PLUS, new RulePlus());
}

public static Rule callRule(Map<RuleType, Rule> ruleMap, RuleType ruleType) {
    return ruleMap.get(ruleType);
}

I have an interface Rule (tried to use an interface but not sure of what I'm doing).我有一个界面规则(尝试使用界面但不确定我在做什么)。

And then I have my different rules.然后我有我不同的规则。 For example:例如:

public class RuleCombo implements Rule {
    public RuleCombo() {

    }

    public static void resolveRule(Tile tilePosee, FrameTT frame, Board board) {
        if (ruleExisting(IDENTIQUE)) {
            RuleIdentique.resolveRule(tilePosee, frame, board);
        }
        if (ruleExisting(PLUS)) {
            RulePlus.resolveRule(tilePosee, frame, board);
        }
        for (Direction direction : Direction.values()) {
            Tile tileDirection = getAdjacentTile(tilePosee, direction, board);
            if (checkTileExist(tileDirection)) {
                if ((tileDirection.getPlayer() != tilePosee.getPlayer()) && checkTileIsOccupied(tileDirection)) {
                    if (ruleExisting(ELEMENTALE)) {
                        RuleElementales.resolveRule(tilePosee, tileDirection, direction, frame, board);
                    } else {
                        cardPoseeWin(tilePosee, tileDirection, direction, frame, board);
                    }
                }
            }
        }
    }
}

Actually the resolveRule() method is static, and it seems to not be that good.实际上resolveRule() 方法是静态的,而且似乎不是那么好。 The thing is, every rule can call another rule.问题是,每个规则都可以调用另一个规则。 That's why I try to use a HashMap and a static method to get my rule Object.这就是为什么我尝试使用 HashMap 和静态方法来获取我的规则对象。

To use it properly I have to write:要正确使用它,我必须写:

RuleCombo ruleCombo = (RuleCombo) callRule(ruleList, COMBO);
ruleCombo.resolveRule(tileDirection, frame, board);

Is there a better way to write it?有没有更好的写法?

Okay so I tried to get some info for chain of responsibility.好的,所以我试图获取一些有关责任链的信息。 I'm not sure but I think I get the idea.我不确定,但我想我明白了。 The thing is, with the kind of drawing I found, I still don't get how I can manage two methods calling each other.问题是,对于我找到的那种绘图,我仍然不明白如何管理两个相互调用的方法。 I have also a method calling itself (it works).我还有一个调用自身的方法(它有效)。 The only thing who can work by deleting the static flag is to put my rules object in argument but it's clearly not the solution.唯一可以通过删除静态标志来工作的方法是将我的规则对象放在参数中,但这显然不是解决方案。

RuleType is an enum, yes. RuleType 是一个枚举,是的。 Sorry I forgot to mention that...对不起,我忘了提...

public enum RuleType { IDENTIQUE, PLUS, COMBO, MEMEMUR, ELEMENTALE }

I think the whole rule part is a mess... I just miss some basics knowledge.我认为整个规则部分是一团糟......我只是错过了一些基础知识。 Maybe I don't need an interface, and every rule is an abstract class.可能我不需要接口,每条规则都是抽象类。 In this case I don't have to create an object from it.在这种情况下,我不必从中创建对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM