Search code examples
design-patternsmvp

mvp design pattern - question


When I have a button which only changes something in my view (e.g. such that some text appears if I press it), can I write its whole code in the file with my view or should I include event handling of this button in the presenter? This is problem for me, because I don't know, if the presenter handles all events from the view or only these, which change something in the model?

Thanks in advance


Solution

  • Presenter only handles the events which change the model. However, each programmer decides himself on what should be put to model, and what belongs entirely to the view.

    Imagine the task, where you need to make 2 buttons. First button loads data from database and shows it, and second button changes the color of the page to some random value. There are 2 ways to implement it:

    1. Model will have LoadedData loadedData and Color color. Presenter would manage both buttons that way.
    2. It could be decided that color is part of the presentation part, and has nothing to do with the model. This way - there would be no color in the model, and all the random color generation and button even handling would appear in the view.

    So, to sum it all up, if you want/need/decide on to put that text into the model - presenter can and should manage that button. But if that text is independent from the main logic and is part of the presentation (some design element) - it should not be put into the model and should not be managed by presenter.