4

I try to work with the MVP Passive View pattern for the first time. I get the basics but some small things I still don't understand.

My example : Imagine a simple view containing a textbox, a button and two labels. When the user clicks the button the text inside the textbox shall be shown in both labels.

Now I have 2 possibilities.

  1. Make a small view interface with the event ButtonClicked, a method GetTextBoxContent and a method SetLabelContents. The presenter then subscribes to the event and handles it by calling the described methods. Here the presenter does not know that there are multiple labels. It just calls the set method once and is done. Therefore the views needs to know how to handle it. Is that okay for the passive view pattern?

  2. Make a big interface with the event ButtonClicked, the methods GetTextBoxContent, SetLabel1 and SetLabel2. The presenter would again subscribe to the event, call GetTextBoxContent and then call each set label method separately. This way the view does not know about having multiple labels and the presenter contains that logic.

Which way is correct / better?

Thanks ob advance!

1 Answer 1

7

Both ways will work, so they are both correct. And none of them is "better" in general, the first one is simpler, but the second one allows some things the first one does not, so you have to analyse your requirements if that additional complexity pays off.

MVP is a design allowing you to test the presenter and mocking out the view easily. It will also allow you to exchange one kind of view by another (for example, a view based on some desktop UI technology by a view based on some web or mobile UI technology). So the question you have to ask yourself here is

  • do you want or need to write unit tests for the "set label" logic, where the tests depend only on the presenter, not on the view?

  • do you want or need to reuse the "set label" logic in two different kind of views?

If the answer to one of this questions is "yes", use your second approach, if you can answer both with "no", the first approach is sufficient.

Note that your artificial, invented example hardly provides enough context to make a decision, you will probably need a real use case to validate your requirements. However, if you are just interested in how the term passive view is defined: Fowler's definition seems to fit better to your second approach, since updating anything in the view (like a label text) should not be done directly by the view itself, but by a controller or presenter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.