代码是
public interface Command {
public Command[] parseCommand(String command);
}
public enum CameraCommand implements Command {
CLICK;
public Command[] parseCommand(String commands) {
if ("CLICK".equals(commands))
return new Command[] { CameraCommand.CLICK };
return null;
}
}
public enum RoverCommand implements Command {
L,
R,
M;
public Command[] parseCommand(String commandString) {
RoverCommand[] commands = new RoverCommand[commandString.length()];
for (int i = 0; i < commandString.length(); i++) {
switch (commandString.charAt(i)) {
case 'L':
commands[i] = RoverCommand.L;
break;
case 'R':
commands[i] = RoverCommand.R;
break;
case 'M':
commands[i] = RoverCommand.M;
break;
default:
break;
}
}
return commands;
}
}
我这样做是为了对命令类型进行分组。 现在问题是,我在字符串中得到一个特定类型的命令,ex“CLICK”。我不知道类型,但我希望这样做
Machine machine = getMachine(type); //machine is an interface, i pass type and get a typeof machine
//machine.parseCommand(commandString); //i don wish to have this logic inside the machine
Command[] command = Command.parseCommand(commandString); // this didnt work, how to solve this, work around?
machine.execute(command); ///finally pass the command and execute the action
任何帮助将不胜感激
感谢 V
答案 0 :(得分:3)
你可以这样做RoverCommand.L.parseCommand
,因为你需要一个Command interfrace实例来调用方法。
但是你应该考虑使用parseCommand
静态方法。例如,在RoverCommand中,将其设置为静态并调用RoverCommand.parseCommand。
我认为你应该用commandString
表示所有命令,例如空格。然后使用一个静态方法解析每个由空格分隔的命令,该方法决定它是“CLICK”还是“L”或“R”或“M”。
E.g。
String commandsString = "L CLICK R L CLICK";
List<Command> commands = CommandParser.parseCommands(commandsString);
public class CommandParser {
public static Command parseSingleCommand(String command) {
if ("CLICK".equals(command)) { return CameraCommand.CLICK; }
else if ("R".equals(command)) { return RoverCommand.R; }
else if ("L".equals(command)) { return RoverCommand.L; }
else if ("M".equals(command)) { return RoverCommand.M; }
else { throw new IllegalArgumentException("Unknown command: " + command); }
}
public static List<Command> parseCommands(String commandsString) {
String[] commands = commandsString.split(" ");
List<Command> result = new ArrayList<Command>();
for (String command : commands) {
result.add(CommandParser.parseSingleCommand(command));
}
return result;
}
}
答案 1 :(得分:1)
您的问题是您在类级别而非实例级别上执行操作。要解决这个问题,你应该声明你的方法解析命令为静态。
public static Command[] parseCommand(String commandString) {
RoverCommand[] commands = new RoverCommand[commandString.length()];
for (int i = 0; i < commandString.length(); i++) {
switch (commandString.charAt(i)) {
case 'L':
commands[i] = RoverCommand.L;
break;
case 'R':
commands[i] = RoverCommand.R;
break;
case 'M':
commands[i] = RoverCommand.M;
break;
default:
break;
}
}
return commands;
}
答案 2 :(得分:1)
我认为你有很多你不需要的锅炉板代码。如果你这样做,你可以添加命令而不必添加代码。
public interface Command {
}
public enum Commands {
;
public static Command[] parseCommand(String command) {
for (Class type : new Class[]{CameraCommand.class, RoverCommand.class})
try {
return new Command[]{(Command) Enum.valueOf(type, command)};
} catch (IllegalArgumentException ignored) {
}
throw new IllegalArgumentException("Unknown Command " + command);
}
}
public enum CameraCommand implements Command {
CLICK
}
public enum RoverCommand implements Command {
L, R, M;
}