MVC / MVVM / MVP
Intent
Separate user-interface concerns — what is shown, what the user does, and how the two stay in sync — so views stay thin and UI logic stays testable without booting a screen.
When It Helps
- The application has significant UI surface (forms, navigation, state updates) that would tangle without a pattern.
- Designers and developers iterate on views while logic must keep working headlessly under test.
- You need to pick the UI wiring that matches your platform’s conventions rather than forcing one shape everywhere.
Structure
flowchart LR
subgraph MVC["MVC"]
U1["View"] --> C1["Controller"]
C1 --> M1[("Model")]
M1 --> U1
end
subgraph MVP["MVP"]
U2["View"] <--> P2["Presenter"]
P2 <--> M2[("Model")]
end
subgraph MVVM["MVVM"]
U3["View"] <-->|data binding| VM3["ViewModel"]
VM3 --> M3[("Model")]
end
Rules
- Views render state and forward user intent; they never contain business rules or direct data access.
- In MVC, controllers handle input and mediate between view and model; the model notifies views of changes.
- In MVP, the presenter owns all view logic behind a view interface, and the view delegates every decision to it.
- In MVVM, the view binds declaratively to an observable view-model; the view-model never holds a reference to view widgets.
Illustrative Snippet
Illustrative pseudocode — not runnable.
# sketch of the view / coordination boundary
view OrderScreen: # all three variants keep this thin
on submit_pressed(items): delegate.submit(items)
show_confirmation(id): display("Order " + id + " placed")
coordinator OrderPresenter_or_ViewModel: # presenter or view-model
function submit(items): # no widget imports here
id = OrderService.place(items)
view.show_confirmation(id) # presenter calls view interface,
# or view-model publishes id for binding
Basic Recap
MVC, MVP, and MVVM separate rendering, user intent, presentation decisions, and domain state in different ways. MVC controllers mediate input, MVP presenters drive a passive view through an interface, and MVVM exposes observable state for declarative binding. None of these variants gives a view permission to own business policy or direct persistence. The best choice usually follows the platform’s rendering and binding model.
Complex Scenario
A retailer maintains a server-rendered catalog, a desktop receiving terminal, and a reactive mobile inventory app. The catalog uses MVC, the terminal uses MVP because its widget toolkit lacks binding, and mobile uses MVVM. All three display the same stock policy through a domain service, but their synchronization mechanisms differ. A new real-time price feed now updates every visible item several times per second.
flowchart LR Browser["Catalog browser"] <--> Controller["MVC controller"] Controller --> Model["Stock and pricing model"] Model --> Controller Terminal["Receiving view"] <--> Presenter["MVP presenter"] Presenter <--> Model Mobile["Mobile view"] <-- "declarative binding" --> ViewModel["MVVM view-model"] ViewModel --> Model Feed["Real-time price feed"] --> Model Model --> Policy["Domain stock policy"]
Scaling walkthrough. A tenfold price-event burst triggers a binding update for every cell, queues work on the mobile UI thread, and retains obsolete view-models through undisposed feed subscriptions. Screens drop frames while memory climbs, and the MVP terminal repeats individual widget updates. Mitigate by conflating updates to the latest snapshot, batching renders at a measured cadence, disposing subscriptions with screen lifecycle, and moving diff computation off the UI thread.
Edge cases — concurrent edits need a visible conflict state, retries need stable submission keys, and out-of-order feed messages need sequence checks before binding. Partial failure should leave the last confirmed model visible with a stale-data indicator rather than clearing the screen.
Sketch (illustrative pseudocode, not runnable):
view_model InventoryScreen:
state: {rows, pending_submission, stale, error}
on_start:
subscription = price_feed
.conflate_to_latest()
.batch_every(100ms)
.observe_on(ui_scheduler)
.subscribe(render_state)
on_stop: subscription.dispose()
presenter ReceivingScreen:
submit(command):
view.show_pending(command.idempotency_key)
result = stock_service.apply(command)
view.render_once(result) # no domain rules in widgets
Trade-offs
- Benefit: UI logic becomes unit-testable and views become replaceable, since rendering is decoupled from decisions.
- Cost: extra types and indirection (presenters, view-models, bindings) that outweigh the gain for trivial screens.
Comparison
| Variant | What it separates | When to choose it |
|---|---|---|
| MVC (Model–View–Controller) | Input handling (controller) from rendering (view) and domain state (model) | Server-rendered or framework-conventional apps where the framework already routes input to controllers |
| MVP (Model–View–Presenter) | View logic behind a view interface so the presenter drives a passive view | Platforms without data binding (e.g. classic desktop, WinForms-style, legacy mobile) where explicit presenter control and unit tests matter |
| MVVM (Model–View–ViewModel) | View state as an observable view-model that the view binds to declaratively | XAML / reactive / modern front-end stacks with data binding, where two-way sync without manual view updates pays off |
When to Use / When Not to Use
Use when:
- UI-heavy client needing separated presentation, state, and view logic per platform norms.
Avoid when:
- Headless service with no UI — the separation adds no value.
Anti-pattern / Failure Mode
- Misuse: Business rules living in controllers/view-models with no testable core.
- Review signal: Massive view-controllers doing persistence and formatting.
Failure Modes / Trade-offs
- Massive coordinator: controllers, presenters, or view-models absorb navigation, validation, caching, and business rules. A coordinator that imports every service is a review warning.
- Two sources of truth: widgets, observable fields, and the domain model each hold editable copies, producing flicker and lost input. Define ownership and one synchronization direction per field.
- Binding opacity: generated subscriptions outlive screens or hide expensive work. Profile allocation and UI-thread time rather than assuming declarative code is cheap.
- Variant trade-off: explicit MVC/MVP wiring is easy to trace but verbose; MVVM reduces manual updates but demands lifecycle and binding expertise.
State review. For every editable field, identify the authoritative owner and when user input is committed; otherwise model refreshes can overwrite work in progress.
Lifecycle review. Test backgrounding, rotation, navigation away, and reconnection because subscriptions and presenter references usually fail at transitions rather than initial render.
Accessibility review. Presentation state should expose progress, stale data, and errors semantically so each view variant can announce them rather than encoding meaning only in color or layout.
- Testing trade-off: passive views and observable state support headless tests, but rendering tests remain necessary for layout, focus, accessibility, and actual binding behavior.
- Performance trade-off: batching reduces render pressure but delays feedback, so critical submission state should bypass the batch used for routine price changes.
- Portability trade-off: sharing a domain model across variants is realistic; sharing widget-specific presenters or view-models usually imports platform assumptions.
Constraint-based use / avoid
| Constraint | Recommendation |
|---|---|
| Framework already provides conventional server controllers | Use MVC instead of introducing a second presentation abstraction. |
| Legacy widgets and a hard requirement for headless UI tests | Use MVP with a narrow passive-view interface. |
| Reactive platform plus frequent, high-volume state changes | Use MVVM cautiously, with update conflation, lifecycle disposal, and frame-time budgets. |
| Two developers maintaining a few static forms | Avoid elaborate triads; simple view code plus domain services has lower cost. |
Related Patterns
- Layered — these UI patterns typically live inside the presentation layer of a layered app.
- Vertical Slice — each slice can own its small MVC/MVVM triad so UI wiring stays feature-local.
- Hexagonal — UI adapters (controllers, presenters, view-models) are the driving adapters of a hexagon.
- Separation of Concerns — the UI form of the split.
- Observer — view updates via broadcasts.