0

我想通过在控制台中输入命令来控制我的服务器,但是因为它不是模块化的,所以编写类似的东西

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

在 Server 类中,如何创建绑定到函数的方法列表,以便将来轻松修改并将其应用于代码的其他部分?

4

2 回答 2

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

...

Action action = actions.get(command);
action.execute();
于 2013-03-09T21:28:55.813 回答
0

我应该寻找现有的设计模式。命令模式符合我的需要。

于 2013-03-09T21:33:18.587 回答