I am implementing a program that takes commands from a user and based on those commands, drives a Robot on a grid. To implement this program I have used the Command pattern. I have used this pattern because, most importantly, I want to be able to add to the list of possible commands in the future.
The program takes a string of commands from the user, and executes those commands, e.g. FFRFLF (forward x 2, turn right, forward, turn left, forward).
Can anyone give me their opinion on the quality of my design?
Robot Controller class:
public void control(){
do{
process();
}while(tru);
}
public String process(){
// 1. Get user input
String cmds = get user input from console
// 2. Populate command list
populateCommandList(cmds);
// 3. Execute commands
executeCommands();
// 4. Display current location of robot
}
public void populateCommandList(String cmds) {
Command r = new TurnRightCommand(robot);
Command l = new TurnLeftCommand(robot);
Command f = new ForwardCommand(robot);
commands = new ArrayList<Command>();
for(int i=0; i<cmds.length(); i++){
char cmd = cmds.charAt(i);
switch(cmd){
case 'F':
commands.add(f);
break;
case 'R':
commands.add(r);
break;
case 'L':
commands.add(l);
break;
}
}
}
private void executeCommands() {
if(commands!=null && commands.size()>0){
for(Command cmd : commands){
cmd.execute();
}
}
}
Main class:
public class Main {
public static void main(String[] args) {
RobotController r = new RobotController();
r.control();
}
}
Is this a good design? I need to know if the deign is good and that I can easily add new commands with minimal rework.