0

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!

Oliver
  • 28
  • 6
  • Every Window is a Group. So in theory, you could create your own Group which can serve as a View, and your Windows can contain several Group implementations... I like the "passive view" type of the MVP pattern myself as well. Problem is that FLTK widgets are tightly bound to the values... It will be really hard to do it with FLTK (not impossible, but just hard). – DejanLekic Jun 26 '14 at 11:56
  • @DejanLekic Thanks for your reply. I know that every Window is a group, but I got a different attempt here, because of the MVP. MVP uses interfaces to increase testability and for better maintenance, so did I. I created Interfaces for the View and the Presenter. Everything works fine, but I´m not sure whether this is good code or not what I´m doing here.. Feels like there is a better way. That´s my main point. – Oliver Jun 26 '14 at 12:09
  • Using interfaces is always a good approach, IMHO. – DejanLekic Jun 26 '14 at 12:53
  • i think it is better to post your design in code blocks, rather than a long story - we all read code better than english ;p) – NirMH Jun 26 '14 at 13:07
  • @NirMH here you go! ;) Edited the question and added the required code parts. – Oliver Jun 26 '14 at 14:19

0 Answers0