MVP

Modelo–Vista–Presentador

Escrito por Darjus Kazakevic · revisado el

Make the view passive, and the screen becomes testable.

MVP changes one thing in MVC: the view stops reading the model. It becomes passive. It shows what it is told and forwards user events. All the logic that decides what to show moves into a presenter.

The presenter talks to the view through an interface, such as showTotal(text). In a test you pass a fake that records the calls. So a screen's whole behaviour runs in milliseconds, with no DOM, emulator or rendering. MVP was common in WinForms, GWT and Android apps before data binding arrived.

En esencia — Move all screen logic into a presenter that drives a passive view through an interface, so you can test the screen's behaviour with no UI framework.

Cuándo recurrir a él

Recurre a él cuando

  • Screen behaviour must be unit-tested without a DOM or emulator
  • The UI toolkit has no data binding (WinForms, classic Android views)
  • Complex forms with many rules about what to enable, show and hide

Reconsidéralo cuando

  • A data-binding framework is available: MVVM removes the push calls
  • Simple screens where the presenter would only forward values
  • Server-rendered pages: each request renders once, so web MVC is simpler

The parts

Passive view
The UI widget or page. It implements the view interface, shows values and forwards events, like a click, to the presenter. It has no conditions about business state.
Presenter
Holds the screen logic: check input, call the model, decide what the view shows. It never imports a widget class, only the view interface.
View interface (the contract)
A small interface in the presenter's terms: showTotal, showError, setCheckoutEnabled. The real widget implements it, and so does the fake in tests.
Model
The data and business rules, as in MVC. The presenter calls it; the view never sees it.

Reglas de diseño

Keep the view passive
The view displays what it is told and forwards events. It never reads the model or decides what to show. Every condition that creeps into it is one your fast tests cannot reach.
Let the presenter own the view interface
Define the interface in the presenter's terms (showTotal, showError) and make the widget implement it. The fake view in your tests is then part of the design.
Never import UI classes into the presenter
No Activity, no DOM element, no Button type. Once the presenter imports one, it can no longer run in a plain unit test.
Detach the view when the screen closes
A presenter often lives longer than its view, for example during an Android rotation. Drop the view reference on close, or updates hit a dead screen and leak memory.

Errores habituales

  • Logic leaking back into the view, such as formatting or hiding fields. Bugs then appear that no presenter test catches.
  • Forgetting to push an update after a change. The screen shows an old total until the next click.
  • A presenter that keeps a reference to a closed view. On Android this crashes or leaks memory after a rotation.