MVP Passive View approach with FLTK

347 Views Asked by At

I have a very basic problem, trying to use FLTK with MVP Passive View as described here. I managed to do it, but it doesn´t feel right the way I´m doing it.

I´m having a Fl_Window, containing some Widgets and a Fl_Gl_Window for OpenGL functionality. As you know, you can add widgets and stuff to a Fl_Window between begin() and end(). And it seems like you have to instantiate everything when you add it directly in between these calls, so I got something like that:

(please look at the small "story" in comments beside the code, because it explains what I´m doing and I want to know if this is fine in this case, or maybe someone can point me the way to a better solution, because it really feels wrong.)

main.cpp

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...
}

View.h

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

View.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
}

IPresenter.h

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

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

IView.h looks the same as IPresenter.h.

Because I want to do MVP, both Windows are Views (one View containing another). Every View needs to communicate with the Presenter and as shown in the sequence diagram by Fowler, the View needs to hold a reference to the Presenter and vice versa. So I used forward declaration in the interface headers.

As already mentioned, I want to know, if there is a better way to do this. Maybe it´s somehow possible to add the Fl_Gl_Window without having to instantiate it at this point. Or maybe my MVP Passive View approach is incorrect.

I hope my explanation is understandable. Thanks in advance for your help!

0

There are 0 best solutions below