Note that there are some explanatory texts on larger screens.

plurals
  1. POApproaches to creating the View for "Humble Dialogs"
    text
    copied!<p>I have a bunch of questions to post regarding the issue of separating the view from logic when creating a GUI.<br> The following is a minimal example of what I would do for a simple dialog that has a label and a button using the "Humble Dialog" approach. Pressing the button should show some text on the label. I have used C++ an Qt that I am comfortable with but I guess it is readable by all other audiences.<br> In any case I am interested in possible side effects because of the choice of language (I am using C++ in the project I am interested in introducing this).</p> <pre><code>class IView { public: IView(){} virtual ~IView(){} virtual void showResult(const QString &amp;text)=0; }; class Presenter { public: Presenter(IView *view){ m_View = view; } ~Presenter(){} void buttonPressed(){ QString text; // Evaluate text m_View-&gt;showResult(text); } private: IView *m_View; } // Multiple inheritance. Is this OK? class MyView : public QDialog, public IView { public: MyView(){ m_Presenter = new Presenter(this); m_Button = new QPushbutton(this); m_Label = new QLabel(this); // Ui event handled inside view but then directly // propagated to the Presenter connect(m_Button,SIGNAL(clicked()),this,SLOT(buttonPressed())); } ~MyView(){ delete m_Presenter; // Qt will automatically delete m_Button and m_Label; } void showResult(const QString &amp;text){ m_Label-&gt;setText(text); } protected slots: void buttonPressed(){ m_Presenter-&gt;buttonPressed(); } private: Presenter *m_Presenter; QPushbutton *m_Button; QLabel *m_Label; } class TestView : public IView { public: TestView(){} ~TestView(){} void showResult(const QString &amp;text){ m_LabelText = text; } QString getResult(){ return m_LabelText; } private: QString m_LabelText; } // Test code TestView view; Presenter presenter(&amp;view); presenter.buttonPressed(); EXPECT_EQ(view.getResult(),"Expected Result"); // Procuction code MyView view; view.show(); </code></pre> <p>Now this is what I got by following the <a href="http://www.objectmentor.com/resources/articles/TheHumbleDialogBox.pdf" rel="nofollow noreferrer">initial work on the Humble dialog by Feathers</a>. The approach that I would get from <a href="http://codebetter.com/blogs/jeremy.miller/pages/129546.aspx" rel="nofollow noreferrer">Fowler's implentation</a> would be to avoid creating the instance of the Presenter class in the constructor of MyView but pass it as a parameter instead so the production code would look like the test code. I personally like the approach I present here.</p> <p>So,</p> <ul> <li>Is it meant to be used with multiple inheritance (see my comment in MyView class)?</li> <li>Should the events be propagated directly to the Presenter or should they be handled in the view that will call the respective presenter action (as I have done here to avoid having to make the Presenter a QObject so it can handle UI events)?</li> <li>Are there any other remarks?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload