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
| Role | Responsible for | What it does not do |
|---|---|---|
| Model | domain data, its state and the rules for changing it | does not know how the data is shown on screen |
| View | the model as a person sees it: a screen, a form, a list, a chart | contains no domain rules |
| Controller | interprets user actions and decides what to do with them | stores 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
| Pattern | How it differs from MVC | Where it is used |
|---|---|---|
| MVP | the presenter receives actions from the view and updates it itself through an interface; the view is passive | desktop and mobile applications |
| MVVM | the view model holds the screen state, and the interface is updated through data binding | WPF, .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- Trygve Reenskaug — MVC: Model-View-Controllerweb.archive.org/web/20200812005121/https://heim.ifi.uio.no/~trygver/(opens in a new tab)
- Apple — Model-View-Controller (MVC)developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html(opens in a new tab)
- Django FAQ — названия model–view–controllerdocs.djangoproject.com/en/stable/faq/general/(opens in a new tab)
Verified 8 Nov