麻烦的多态性打败那些开关

麻烦的多态性打败那些开关

本文介绍了麻烦的多态性打败那些开关/ case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 继续以前的问题(此处和这里 ),我实现了一个基本的命令模式,创建了我的命令类并编码到一个接口,所以当使用任何命令时,调用 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.但是,我仍然发现自己无法摆动这些case语句:我从一个主/决定字符串读取每个字符,它由随机,重复的字符组成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.我的设计是这样的: p>My design was like this: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);但是,当我阅读每个指令时,我再次诉诸于case语句: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;}显然,在某种程度上,我设法打破了多态性与案例陈述的优势。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推荐答案我可能在这里缺少一些东西,而不是开关语句,I may be missing something here, but instead of the switch statement, what's wrong with((Command)myMap.get(instructionFromString)).execute();如果instructionFromString是一个 char ,则转换在进行地图查找之前,将其转换为 String ,否则请在地图中使用字符键。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(); 这篇关于麻烦的多态性打败那些开关/ case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 09:48