the code is
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;
开发者_如何学Go case 'R':
commands[i] = RoverCommand.R;
break;
case 'M':
commands[i] = RoverCommand.M;
break;
default:
break;
}
}
return commands;
}
}
I did this to group the commands type. now the problem is, I get a command of particular type in string, ex "CLICK". I do not know the type, but I wish to do like this
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
any help would be appreciated
thanks V
You can do like RoverCommand.L.parseCommand
, because you need an instance of Command interfrace to call method.
But you should consider making parseCommand
static method. For example in RoverCommand make it static and call RoverCommand.parseCommand.
I think you should spearate all commands in commandString
by for example spaces. And then parse every single command, delimited by spaces, using one static method, which decides if it is "CLICK" or "L" or "R" or "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;
}
}
Your problem is that you are acting on class level not instace level. TO solve this you should declare your method parseCommand as static.
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;
}
I think you have alot of boiler plate code you don't need. If you do it this way you can add Commands without having to add code as well.
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;
}
精彩评论