麻烦使多态性打败那些 switch/case 语句

继续前面的问题(此处,以及这里),我实现了一个基本的命令模式,创建了我的命令类并编码到一个接口,所以当使用任何命令时,调用 execute() 方法.

Continuing on previous questions (here, and here), I implemented a basic command pattern, created my command classes and coded to an interface, so when any command is used, to call the execute() method.

但是,我仍然发现自己无法动摇这些案例陈述:我正在从一个主/决策字符串中读取每个字符,该字符串由随机重复的字符 A、B、C 或 D 组成,然后我检索从地图中执行命令的相关实现并调用其执行方法.

However, I am still finding myself unable to shake those case statements: I am reading each character from a master/decision String, which is made up of random, repeating characters A, B, C or D, and then I retrieve the related implementation of the command from a map and call its execute method.

我的设计是这样的:

public interface Command {
    void execute();
}

public class CommandA implements Command{
  //implements execute() method
}

private Map myMap= new HashMap();
myMap.put("A", new CommandA);
myMap.put("B", new CommandB);
myMap.put("C", new CommandC);
myMap.put("D", new CommandD);

但是,当我阅读每条指令时,我又不得不求助于案例陈述:

But, then when I read each instruction, again I have to resort to case statements:

switch(instructionFromString){
case 'A':{myMap.get("A").execute(); break;}
case 'B':{myMap.get("B").execute(); break;}
case 'C':{myMap.get("C").execute(); break;}
case 'D':{myMap.get("D").execute(); break;}

显然,在此过程中,我设法克服了多态性对 case 语句的优势.

Obviously, somewhere along the way I managed to defeat the advantage of polymorphism against the case statements.

可能是我选择用来存储命令的那种数据结构吗?它很可能是一个永久的数据结构,可以从中提取这些命令.

Could it be the kind of data structure I selected to store my commands? It can very well be a permanent data structure to just pull those commands from.

我想到的另一件事是我在地图中使用的键/值命名.我试图在概念上将每个存储的命令链接到其相关指令的方式?即命令A"的实现,用键A"存储在地图上,这样它就可以匹配相应的指令A"?不过在我看来这有点不太可能.

Another thing that comes to mind is the key/value naming that I used in my map. The way I tried to conceptually link each stored command to its associated instruction? i.e. Implementation of command "A", is stored on the map with key 'A' so it can match to the corresponding instruction "A"? That seems to me a bit unlikely though.

任何关于我下一步一劳永逸地删除这些案例陈述的提示或进一步建议将不胜感激.非常感谢提前

Any hint or further advice about my next move to remove those case statements once and for all would be highly appreciated. Many thanks in advance

推荐答案

我可能在这里遗漏了一些东西,而不是 switch 语句,有什么问题

I may be missing something here, but instead of the switch statement, what's wrong with

((Command)myMap.get(instructionFromString)).execute();

如果 instructionFromString 是一个 char,则在进行地图查找之前将其转换为 String,否则使用地图中的 Character 键.

If instructionFromString is a char, the convert it into a String before doing the map lookup, else use Character keys in your map.

此外,如果您使用 Java 5 通用映射,则可以移除对 Command 的强制转换.清理后的版本是:

Also, if you use a Java 5 generic map, then you can remove the cast to Command. A cleaned up version would be:

private Map<Character, Command> myMap = new HashMap<Character, Command>();
myMap.put('A', new CommandA());
myMap.put('B', new CommandB());
myMap.put('C', new CommandC());
myMap.put('D', new CommandD());

接着是:

char instructionFromString = ....
myMap.get(instructionFromString).execute();

相关文章