Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 57 ยท Architectural and Modern Patterns

MVC, MVP and MVVM

Compare MVC, MVP and MVVM presentation patterns: what each part does, how data flows, and where each fits, with a runnable tiny MVC app and a view-model.

User interface code has a habit of becoming a mess: the screen, the rules and the data all end up in one giant file. MVC, MVP and MVVM are three well-known ways to split that work into separate parts so each can change, and be tested, on its own. They are not rival products; they are variations on one idea, "keep the display apart from the logic", and they differ mainly in what sits in the middle.

You will meet them everywhere: MVC in Rails, Laravel, Django (as MTV) and Spring MVC; MVP in classic Android and Windows Forms code; MVVM in WPF, SwiftUI, Vue and many Angular or Android apps. This lesson explains the roles and shows working code.

The three parts

Model Data and business rules.
View What the user sees and clicks.
The middle Controller, Presenter or ViewModel.

The patterns differ in the third card.

1 / 2

The shared idea: separate presentation from logic

All three patterns split the application into at least these responsibilities:

  • Model. The data and the rules: a Todo with a title and a done flag, a TodoList that can add and complete items. It knows nothing about screens, so you can test it without any interface.
  • View. The visible interface. It shows data and reports user actions (clicks, typing). It should contain as little decision-making as possible.
  • Mediating piece. Something that connects the two: reacts to user actions, updates the model, and makes the view show the new state.

This separation gives you tests that need no screen, the freedom to swap the view (web, mobile, terminal) over the same model, and a place for every kind of code. It is a close cousin of the Single Responsibility Principle.

MVC: Model, View, Controller

In classic MVC, the controller receives user input, tells the model what to do, and chooses which view to show. The view reads from the model to render itself, often by observing it. So the view may know about the model, while the model knows nothing about either.

flowchart LR
    U["User"] -->|"input"| C["Controller"]
    C -->|"update"| M["Model"]
    M -.->|"notifies"| V["View"]
    V -->|"reads state"| M
    V -->|"shows"| U

The model notifies the view when it changes (an Observer relationship), so the controller does not have to push data.

Server-side web frameworks use a request-driven flavor: the controller handles a request, asks the model, and picks a view template to render a response. Here is a tiny working version with a console view:

JS A tiny MVC app

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The model has no idea a console exists. You could replace ConsoleView with an HTML view without touching TodoModel. This is a stand-in for a real UI: the "clicks" are method calls, but the roles and data flow are the real ones.

MVP: Model, View, Presenter

MVP makes the view passive. The view knows nothing about the model; it only exposes simple methods such as showTodos(list) and showError(text), and forwards user events to the presenter. The presenter does all the thinking, reads the model, and tells the view exactly what to display.

The big win is testability. The presenter depends on an interface for the view, so a test supplies a fake view and checks which methods were called, with no screen at all. The cost is more code: lots of small "show this" methods and a presenter that grows.

The classic variants are Passive View (the view has no logic at all) and Supervising Controller (the view may do simple data binding while the presenter handles complex logic).

MVVM: Model, View, ViewModel

MVVM replaces the presenter with a view model: an object that exposes the view's state and commands as properties, such as visibleTodos, newTitle, canAdd. The view binds to those properties through a framework, so when the view model changes, the screen updates automatically, and when the user types, the view model changes. The view model never refers to the view at all.

The binding is the crucial ingredient. WPF has XAML bindings, SwiftUI has @Published and ObservableObject, Vue and Angular have templates, and Android has LiveData or StateFlow. Without a binding mechanism, MVVM degenerates into MVP.

The next TryIt shows the idea with a hand-written observable. The "binding" at the bottom is a stand-in for what a framework does for you:

JS A view model with simple data binding

Press Run (or Ctrl + Enter) to see the console output here.

No output.

You can test SignupViewModel the same way you would test any object: set the observables, call submit(), and check the result. No screen is involved.

Choosing between them

MVC MVP MVVM
Middle part Controller Presenter View model
View knows the model? Often yes (reads or observes it) No No
Middle part knows the view? Loosely Through an interface Never
Update mechanism Observer or re-render Presenter calls view methods Data binding
Strength Simple, familiar, fits request/response Very testable, explicit Little glue code, reactive UIs
Weakness Controllers can bloat Many boilerplate methods Needs a binding system; hard to debug bindings

Practical advice:

  • Server-rendered web apps (Laravel, Rails, Django, Spring MVC) use request-driven MVC. Keep controllers thin and put rules in models or services.
  • Desktop and mobile apps with a binding framework (WPF, SwiftUI, Jetpack Compose with a ViewModel, Vue) fit MVVM.
  • UIs without bindings, or when you need maximum testability, fit MVP.
  • Modern front ends add ideas such as unidirectional data flow (Redux, Elm) that sit near MVVM. The terms are loose, and many "MVC" apps are really something in between. Learn the roles, not the label.

A common mistake in all three is putting business rules in the middle layer. Controllers, presenters and view models should coordinate; rules like "an order cannot exceed the credit limit" belong in the model or domain layer, perhaps behind a Repository. The Observer pattern is the engine behind both the MVC model-to-view notification and MVVM binding, and Strategy is often used to swap controllers or views.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which part of MVC contains the business data and rules and knows nothing about the screen?

    The model holds data and rules and is independent of how they are displayed, which keeps it testable and reusable.
  2. 2. What distinguishes MVP from MVC?

    In MVP the view does not read the model; the presenter pushes everything through simple view methods, which makes the view easy to replace with a fake in tests.
  3. 3. What does MVVM rely on to keep the view and the view model in step?

    A framework binds view elements to view model properties, so changes flow both ways without the view model referring to the view.
  4. 4. A controller contains a long method that checks credit limits and computes discounts. What is wrong?

    Putting rules in controllers makes them hard to reuse and test. Keep the controller thin and delegate to the model.
  5. 5. Which style fits a server-rendered Laravel, Rails or Spring MVC application?

    These frameworks route a request to a controller, which uses models and chooses a view template to produce the response.

Summary

  • MVC, MVP and MVVM all separate model (data and rules), view (display) and a mediating piece.
  • MVC: the controller handles input and updates the model; the view observes or reads the model.
  • MVP: a passive view is driven entirely by the presenter; best for testability.
  • MVVM: the view model exposes state and commands and the view binds to them automatically.
  • Keep business rules in the model, not in controllers, presenters or view models.
  • The labels are loose in practice; understand the roles and data flow.

Next lesson: Dependency Injection, Service Locator and Plugin Architectures.