0

I want to control my server by typing commands in the console, but since it is not modular to write something like

switch(command)
{
  case 'command1':
    command1();
    break;
  /* And so on */
}

in the class Server, how can I create a list of methods bound to functions, that I can easily modify in the future and apply it to other parts of my code ?

  • 1
    create a interface class, derive a bunch of classes for each command with a common method, put them in a Map object, lookup by key, call method. – OldProgrammer Mar 09 '13 at 21:27

2 Answers2

4
Map<String, Action> actions = new HashMap<String, Action>();
// TODO populate the map

...

Action action = actions.get(command);
action.execute();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I should have looked out for an existing design pattern. Command pattern corresponds to my need.