c++ - GUI 和文本模式 C++ 设计以消除冗余(可选参数?函数重载?)

标签 c++ user-interface text

使用 C++,我有一个文本模式和一个 GUI 模式,使用由命令行选项选择的 FLTK 实现,我看到代码中有很多冗余,除了 GUI 案例中我需要在主程序中传递的额外参数窗口小部件。我想知道是否有办法消除冗余?也许我有一些设计问题完全错误?非常感谢您的帮助,如果需要其他信息,请告诉我。我想过使用可选参数,但不能采用 NULL 引用。

这是真正相似的代码框架(不准确,但应该足够接近以查看一般结构)。在我使用额外的 Window& 参数进行一次不同的调用之前,可能还嵌套了一些 if/else 循环或函数,但这基本上是结构,实际上继续向下几层。

感谢您的帮助!

int Game::init(){
  if (graphics){
    std::unique_ptr<Window> window = std::unique_ptr<Window>(new Window(...))
    return Fl::run();
  } else {
    play_game();
    return 0;
  }
}

void Window::init(Fl_Widget* w, void *uData){
    Window* window = (Window*) uData;
    Window->game.play_game(window);
    //Window has a private game& that is constructed to be equal to the game above.
}

void Game::play_game(){
    while(!over()){
       foo();
       bar();
    }
}

void Game::play_game(Window& window){
    while(!over()){
       foo();
       bar(window);
    }
}

void Game::bar(){
   if(!a()){
      b();
   } else {
      c();
   }
}

void Game::bar(Window& window){
   if(!a()){
      b();
   } else {
      c(window);
      window.redraw();
   }
}

一个类似但不同的问题涉及我如何处理 FLTK 中的静态函数,我在某处有类似的代码,如下所示:

void Game::c(){
  if(check_this()){
    do_this();
  }
}

void Game::c(Window& window){
  Fl::run();
}

static void Window::call_back(Fl_Widget* w, void* uData){
  Window* window = (Window *) uData;
  if(window->game.check_this()){
    window->do_this();
  }
}

最佳答案

参数不是要走的路。子类化 Game 是可行的方法。任何需要访问窗口的方法都是虚拟的,并在特定于窗口的子类中被适本地覆盖。

class Game        // I hate K&R braces, sorry
{
public:
    enum GameType { cli, win };
    static Game &GameFactory(GameType gt)
    {
        switch (gt)
        {
        case cli: return /* ref to instance of CliGame() */;
        case win: return /* ref to instance of WinGame() */;
        }
    }

    virtual int launch() = 0;
    void foo();
    void bar()
    {
       if (!a()) { b(); } else { c(); }
    }
    bool a();
    void b();
    virtual void c();
    void play()
    {
        while (!over()) { foo();  bar(); }
    }
private:
    // need some sort of static management of instance of game, how is up to you
};

class WinGame : public Game
{
public:
    virtual int launch()
    {
        window = std::unique_ptr<Window>(new Window(...));
        return Fl::run();    // presumably calls play_game() sometime....
    }
protected:
    virtual void c()
    {
        // does whatever, using window *member* (not argument)
        window.redraw();
    }
private:
    std::unique_ptr<Window> window;
};

class CliGame : public Game
{
    virutal int launch()
    {
        play_game();
        return 0;
    }
    virtual void c()
    {
    // does whatever
    }
};

int main()
{
    Game::GameType graphics;
// 'graphics' gets set somehow
    Game &g = Game::GameFactory(graphics);
    int retval = g.launch();
// etc
}

关于c++ - GUI 和文本模式 C++ 设计以消除冗余(可选参数?函数重载?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381643/

相关文章:

user-interface - 是否可以将变量传递给 WIX 本地化文件?

php - 在离线/桌面应用程序中呈现网页内容

c++ - 在 C/C++ 中实现导数

c++ - cstdint 之外的数字类型

c++ - 试图将位图复制到 WMP 渲染器中 -> 颠倒了!

H3 标签中的 jQuery 值替换为文本框中的值

javascript - Canvas 不创建文本

c++17 如何编写 is_pointer_pointer 泛型 lambda?

java - 具有排列的多个输入的 JOptionPane

php - 使用 php 在文本文件中搜索