Jump to content

Model View Controller

Википедиа — Чөлөөт нэвтэрхий толь
06:48, 31 Тавдугаар сар 2010-ий байдлаарх Khishig.s (хэлэлцүүлэг | оруулсан хувь нэмэр) хэрэглэгчийн хийсэн залруулга
Model-View-Controller concept. The solid line represents a direct association, the dashed an indirect association via an observer (for example).

Model–View–Controller (MVC) is a software architecture[1], currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" (the application logic for the user) from input and presentation (GUI), permitting independent development, testing and maintenance of each.

The model is used to manage information and notify observers when that information changes. The model is the domain-specific representation of the data upon which the application operates. Domain logic adds meaning to raw data (for example, calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items). When a model changes its state, it notifies its associated views so they can be refreshed.

Many applications use a persistent storage mechanism such as a database to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the model. Models are not data access objects; however, in very simple apps that have little domain logic there is no real distinction to be made. Also, the ActiveRecord is an accepted design pattern which merges domain logic and data access code - a model which knows how to persist itself.

The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.

The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

Model View Controller Process
Model View Controller Process.

An MVC application may be a collection of model/view/controller triplets, each responsible for a different UI element.

MVC is often seen in web applications where the view is the HTML or XHTML generated by the app. The controller receives GET or POST input and decides what to do with it, handing over to domain objects (i.e. the model) that contain the business rules and know how to carry out specific tasks such as processing a new subscription.

History

MVC was first described in 1979[2] by Trygve Reenskaug, then working on Smalltalk at Xerox PARC. The original implementation is described in depth in the influential paper "Applications Programming in Smalltalk-80: How to use Model–View–Controller".[3]

There have been several derivatives of MVC. For example, Model View Presenter is used with the .NET Framework[4], and the XForms standard uses a "model-view-controller-connector architecture"[5]. However, standard MVC remains popular.[баримт хэрэгтэй]

Overview

Though MVC comes in different flavors, control flow is generally as follows:

  1. The user interacts with the user interface in some way (for example, presses a mouse button).
  2. The controller handles the input event from the user interface, often via a registered handler or callback and converts the event into appropriate user action, understandable for the model.
  3. The controller notifies the model of the user action, possibly resulting in a change in the model's state. (For example, the controller updates the user's shopping cart.)[6]
  4. A view queries the model in order to generate an appropriate user interface (for example, the view lists the shopping cart's contents). The view gets its own data from the model. The controller may (in some implementations) issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) which require a screen update.
  5. The user interface waits for further user interactions, which restarts the cycle.

Some implementations such as the W3C XForms also use the concept of a dependency graph to automate the updating of views when data in the model changes.

The goal of MVC is, by decoupling models and views, to reduce the complexity in architectural design and to increase flexibility and maintainability of code.

Selected frameworks

GUI frameworks

VCL, CLX, Cocoa, CocoaTouch

Controllers.
The controller is represented by the visual forms created either in the form designer or via code.

Combined frameworks

Java: Java Platform, Enterprise Edition (Java EE)

Simple Version implementing Java Servlets and JavaServer Pages from Java EE:

Model
The model is a collection of Java classes that form a software application intended to store, and optionally separate, data. A single front end class that can communicate with any user interface (for example: a console, a graphical user interface, or a web application).

See also

mn:Model View Controller

  1. Reenskaug, Trygve. "MVC XEROX PARC 1978-79". Татаж авсан: 2008-06-09.
  2. Trygve M. H. Reenskaug/MVC—XEROX PARC 1978-79
  3. How to use Model–View–Controller (MVC)
  4. Boodhoo, Jean-Paul (August 2006). "Design Patterns: Model View Presenter". Татаж авсан: 2009-07-07.
  5. World Wide Web Consortium (December 9, 2008). "The Forms Working Group". Татаж авсан: 2009-07-07.
  6. Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.