0

我有一个非常基本的问题,尝试将 FLTK 与 MVP Passive View 一起使用,如所述所述。我设法做到了,但我这样做的方式感觉不对。

我有一个Fl_Window, 包含一些小部件和一个Fl_Gl_Window用于 OpenGL 的功能。如您所知,您可以将小部件和东西添加到Fl_Windowbetweenbegin()end(). 当你在这些调用之间直接添加它时,似乎你必须实例化所有东西,所以我得到了类似的东西:(

请查看代码旁边注释中的小“故事”,因为它解释了我在做什么和我想知道在这种情况下这是否可以,或者也许有人可以为我指出更好的解决方案,因为它真的感觉不对。

主文件

int main(/*arguments*/) {
    Model* model = new Model();
    IPresenter* presenter = new Presenter(model); //Presenter only knows the model at this point
    IView* view = new View(presenter);           //View gets reference to the presenter...
}

查看.h

class View : public Fl_Window {
public:
    View(IPresenter* presenter);
    virtual ~View();
private:
    IView* gl_window;
    IPresenter* presenter;
};

查看.cpp

View::View(IPresenter* presenter) :Fl_Window(/*arguments*/) {
    this->presenter = presenter;
    begin();
    //add some Widgets
    gl_window = new GlWindow(this->presenter,/*more arguments*/); //...instantiates GlWindow and passes the received reference to it...
    end();
    show();
    Fl::run();
}

GlWindow.h

GlWindow::GlWindow(IPresenter* presenter, /* more arguments*/) :
    Fl_Gl_Window(/*arguments*/), IView() {
    //initialisations
    this->presenter = presenter;      //...which GlWindow uses for communication and...
    this->presenter->setView(this);   //...to set itself as reference to the presenter
                                      //same with View
}

演示者.h

class IView;   //forward declaration because IPresenter includes IView and vice versa

class IPresenter {
public:
    //pure virtual methods
};

IView.h 看起来与 IPresenter.h 相同。

因为我想做MVP,两个窗口都是视图(一个View包含另一个)。每个View需要与Presenter和 通信,如序列图中所示FowlerView需要持有对 的引用Presenter反之亦然。所以我在接口头中使用了前向声明。

如前所述,我想知道是否有更好的方法来做到这一点。Fl_Gl_Window也许在此时无需实例化它就可以以某种方式添加。或者也许我的MVP Passive View方法不正确。

我希望我的解释是可以理解的。在此先感谢您的帮助!

4

0 回答 0