在C ++中创建交互式提示

Dim*_*782 4 c++ oop parameters design-patterns

我有一个程序应从控制台读取命令,并根据命令执行以下几种操作之一。这是我到目前为止的内容:

void ConwayView::listening_commands() {
    string command;

    do {
        cin >> command;

        if ("tick" == command)
        {
            // to do
        }
        else if ("start" == command)
        {
            // to do for start
        }
        ...

    } while (EXIT != command);
}
Run Code Online (Sandbox Code Playgroud)

如果存在大量命令switch,则使用代替if语句会有所帮助。您建议使用什么模式来提供交互式命令行?

pmr*_*pmr 6

并不是真正的模式,但通常是一个很好的方法:

#include <map>
#include <functional>
#include <string>
#include <iostream>

typedef std::map< std::string, std::function<void(void)> > command_dict;
//                                           ^^^^^^^^
//                               the signature of your commands. probably should have an error code.

void command1() { std::cout << "commanda" << std::endl; }
void command2() { std::cout << "commandb" << std::endl; }
void command3() { std::cout << "commandc" << std::endl; }

int main() {
  command_dict c;
  c["a"] = &command1;
  c["b"] = &command2;
  c["c"] = &command3;

  std::string input;
  while(std::getline(std::cin, input)) { // quit the program with ctrl-d
    auto it = c.find(input);
    if(it != end(c)) {
      (it->second)(); // execute the command
    } else {
      std::cout << "command \"" << input << "\" not known" << std::endl;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


mah*_*mah 5

有多种解决方案,“正确”的解决方案是有争议的。如果我要为自己的工作解决它,我将创建一个自定义结构的表。就像是:

struct CommandStruct {
    char *command;
    int (*commandHandler)(/*params*/);
} commandTable[] = {
    { "tick",  tickCommand },
    { "start", startCommand },
    ...
};
Run Code Online (Sandbox Code Playgroud)

然后,我的处理循环将遍历该表的每个元素,寻找正确的匹配项,例如:

for (int i = 0; i < TABLE_SIZE; ++i) {
    if (command == commandTable[i].command) { /* using whatever proper comparison is, of course */
        commandTable[i].commandHandler(/*params*/);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*and 4

如果命令数量很少,可能的参数也很少,你可以继续使用 switch case !

如果命令数量增加,请考虑命令设计模式(恕我直言,这是某种伪装的策略模式:参见使用策略模式和命令模式来了解命令模式和策略模式之间的差异)。

如果您的大多数命令都共享相同行为的一部分,请不要忘记模板方法模式

如果创建命令对象的复杂性增加(即解码/理解命令行输入的复杂性),您应该开始考虑解释器设计模式

如果在解释器模式的帮助下进行设计时,您碰巧看到了一些复杂性(如果解释器需要太多工作,您会看到语法问题等等),那么您可能应该考虑DSL,领域特定语言,并设计您的适合(并且仅适合)您自己的输入的自己的语言。