Bookmarks

MVC — model–view–controller

A pattern for building user interfaces that splits a program into the model (data and rules), the view (display) and the controller (handling of user actions).

Also: MVC, model-view-controller, MVC pattern

checked 8 Nov

All terms

MVC answers the question of how to divide the code of an interactive program so that the data, the way it is shown on screen and the response to user actions can each change independently. The pattern was devised for desktop Smalltalk, but its variations underlie most web frameworks and mobile platforms.

Three roles

RoleResponsible forWhat it does not do
Modeldomain data, its state and the rules for changing itdoes not know how the data is shown on screen
Viewthe model as a person sees it: a screen, a form, a list, a chartcontains no domain rules
Controllerinterprets user actions and decides what to do with themstores no data and draws no interface

Every object in the program takes one of the three roles and talks to the rest only across their boundaries. Roles are not tied to classes: the same object may play several roles, and one role may be shared by several objects.

How a user action flows

  • The user acts: presses a button, types text, sends a request.
  • The controller interprets the action and gives the model commands: change a value, save, compute.
  • The model changes the data and notifies the views that observe it.
  • The views read the new values from the model and update the screen.

In web frameworks the division is the same in spirit, but the names have shifted: in Django the framework itself plays the role of the controller and hands the request to the “view”; the “view” decides which data to show, and the template decides how to show it.

Where the term comes from

The pattern was devised by Trygve Reenskaug in 1979 at Xerox PARC and described in a memo for programmers. The problem he was solving: to give the user control over their own data, visible from different points of view, and a sense of dealing with it directly.

Descendants: MVP and MVVM

PatternHow it differs from MVCWhere it is used
MVPthe presenter receives actions from the view and updates it itself through an interface; the view is passivedesktop and mobile applications
MVVMthe view model holds the screen state, and the interface is updated through data bindingWPF, .NET MAUI and platforms with declarative interfaces

MVC is one of the most retold and at the same time most distorted patterns: a single name hides different arrangements, from Application Model to MVVM. So when discussing a particular system it is worth clarifying who is responsible for what.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • MVVMmodel — view — viewmodel

    In MVVM a view model takes the place of the controller, and data binding updates the interface without manual code.

  • layered architecture

    Layers divide the whole system by dependency: data access, logic, interface; MVC is about roles within a single interface.

In depth in the references

where the topic is covered in detail