Overview
Though MVC comes in different flavors, control flow is generally as follows:
- The user interacts with the user interface in some way (for example, by pressing a mouse button).
- The controller handles the input event from the user interface, often via a registered handler or callback, and converts the event into an appropriate user action, understandable for the model.
- 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.)[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. In some implementations, the controller may 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) that require a screen update.
- The user interface waits for further user interactions, which restarts the control flow 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.
Concepts
The model manages the behaviour and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.
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 user 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.
An MVC application may be a collection of model/view/controller triads, each responsible for a different UI element. The Swing GUI system, for example, models almost all interface components as individual MVC systems.
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, and which hand control to (X)HTML-generating components such as templating engines, XML pipelines, Ajax callbacks, etc.
The model is not necessarily merely a database; the 'model' in MVC is both the data and the business/domain logic needed to manipulate the data in the application. 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. Active Record is an accepted design pattern that merges domain logic and data access code — a model which knows how to persist itself.
Architecture vs. frameworks
Although MVC is typically associated with frameworks, it is essentially an architecture.[6] This means that it can be implemented even without an object-oriented language or a specific class hierarchy. For example, using as little as jQuery's trigger() and bind(), it is possible to build robust MVC applications in a browser using Javascript. The key is simply to divide up the responsibilities of the MVC components into clearly defined sections of code. As stated in the overview, the code that embodies the model takes care of state, business logic, persistence, and notifications. The persistence can be implemented via cookies or AJAX. The notifications can be taken care of by the jQuery.trigger(). The code that embodies the view takes care of querying the model and rendering the view. The view code can be implemented in a variety of ways, including inserting HTML DOM nodes or changing CSS styles. The code that embodies the controller takes care of initialization of the model and wiring up the events between the view's HTML DOM elements and controller and between the model and the view code, using jQuery.bind().
Example
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).
View
The view is represented by a JavaServer Page, with data being transported to the page in the HttpServletRequest or HttpSession.
Controller
The Controller servlet communicates with the front end of the model and loads the HttpServletRequest or HttpSession with appropriate data, before forwarding the HttpServletRequest and Response to the JSP using a RequestDispatcher.
Implementations of MVC as GUI frameworks
- GNUstep, also based on OpenStep, encourages MVC as well.
- GTK+ provides models (as both interfaces and as concrete implementations) and views, while clients implement the controllers through signals.
- JFace.
- Qt since Qt4 release.
- Java Swing.
- Apache Pivot.
- Adobe Flex with the Cairngorm Framework.
沒有留言:
發佈留言