Model-View-Presenter (MVP)

A derivative of MVC where the Controller is replaced by the Presenter.

“The Presenter assumes the functionality of the ‘middle-man’. All presentation logic is pushed to the Presenter.”

Used heavily in older desktop and Android dev.

graph LR
    User -->|Interacts| V[View]
    V -->|Events| P[Presenter]
    P -->|Updates| M[Model]
    M -->|Data| P
    P -->|Updates UI| V

When to use

  • Desktop Apps: WinForms, Swing.
  • Testability: You want to mock the View interface to test UI logic (the Presenter).

Explanation

Problem

In MVC, the View often has knowledge of the Model (Active View). This makes the View hard to test.

Solution

  • View: Passive Interface. Has no logic. Forwards events to Presenter.
  • Presenter: Decisions. Retrieves data from Model, formats it, and updates View.
  • Model: Data.

Real world analogy

TV News:

  • News Anchor (Presenter) reads the news.
  • Prompter/Reporter (Model) gathers the raw text/facts.
  • Screen (View) just shows what the anchor says. Anchor decides how to say it.

Pros and Cons

Pros Cons
  • View is very dumb (Passive View)
  • Presenter becomes huge (God Class)
  • High Testability of Presenter
  • Boilerplate (Interfaces for Views)
  • Code example

    Typescript

    PHP