Search code examples
design-patternsmvp

Can the Presenter have state? (MVP pattern)


I'm reading about the Model-View-Presenter pattern, and I'm a bit confused: can a presenter have state? Or is the task of the Presenter to only group methods together containing business logic, and every state information should be put in the Model?

Here's an example scenario: suppose I have to present a sortable, paged list of items. In this case the View should view the items. The presenter should load the items to be viewed into the model. Where should the 2 state variables (sorting type and the current page index) go, should they be considered the state of the Presenter or part of the model?


Solution

  • This is clearly not an application state but a view state. Not only losing this state won't hurt much but also it does not affect the business logic.

    That being said I wouldn't hesitate to place this extra "state" in the presenter or even in the view. You will probably implement this using either HTTP session or client-side cookies - these persistence strategies obviously are targeted for view.