Вы находитесь на странице: 1из 3

Filire d'ingnieur d'tat en Gnie logiciel

FSTS 2014/2015

Model-View-Controller (MVC)
Context
In systems involving user interfaces, the following situation typically arises:

The system has to accept data from the user, update the database, and return
the data to the user at a later point in time.
There are several ways in which the data can be accepted from and presented
to the system users.
Data fed to the system in one form should be retrievable in another form.

Problem
If the system deploys a single component that interacts with the user as well as
maintains the database, then a requirement to support a new type of display or
view will necessitate the redesign of the component.

Example
Suppose a bank provides online stock trading facilities. When the user is logged
into the site, the web application allows the user to view the rates of the stocks
over a period of time in various ways, such as a bar graph, a line graph, or a plain
table. Here, the same data that represents the rates of the stocks is viewed in
multiple ways, but is controlled by a single entity, the web application.

Facts or forces to consider


In the context and the problem presented above, we observe the following facts:

There are three tasks to be done:


1. Manage the users interaction with the system.
2. Manage the actual data.
3. Format the data in multiple ways and present it to the user.
Thus, a single component that does all the tasks can be split into three
independent components.
All three tasks can then be handled by different components.

Solution
The solution is to separate the data presentation from the data maintenance and
have a third component that coordinates the first two. These three components
are called the Model, the View, and the Controller, and they form the basis of the
MVC pattern. Figure 18.1 shows the relationship between the components of the
MVC pattern. Here are the responsibilities of the three MVC components:

Model: The Model is responsible for keeping the data or the state of the
application. It also manages the storage and retrieval of the data from the
data source. It notifies all the Views that are viewing its data when the data
changes.
View: The View contains the presentation logic. It displays the data contained
in the Model to the users. It also allows the user to interact with the system
and notifies the Controller of the users actions.
Controller: The Controller manages the whole show. It instantiates the Model
and the View and associates the View with the Model. Depending on the
application requirements, it may instantiate multiple Views and may associate

Filire d'ingnieur d'tat en Gnie logiciel


FSTS 2014/2015
them with the same Model. It listens for the users actions and manipulates
the Model as dictated by the business rules.

Consequences/implications

Separating the data representation (Model) from the data presentation (View)
allows multiple Views for the same data. Changes can occur in both the Model
and View components independently of each other as long as their interfaces
remain the same. This increases maintainability and extensibility of the
system.
Separating the application behavior (Controller) from the data presentation
(View) allows the controller to create an appropriate View at runtime based on
the Model.
Separating the application behavior (Controller) from the data representation
(Model) allows the users requests to be mapped from the Controller to
specific application-level functions in the Model.

Category
Although MVC involves communication between the Model, View, and Controller
components, it is not a behavioral pattern because it does not specify how the
three components should communicate. The MVC pattern only specifies that the
structure of a system of components be such that each individual component
take up one of the three rolesModel, View, or Controllerand provide
functionality only for its own role. As such, it is a structural pattern.
In the J2EE world, MVC is thought of more as an architecture rather than a design
pattern. Though it can be applied in any of the tiers, it is most suitably applied in
the presentation tier.

Points to remember
The Model-View-Controller design pattern, consisting of three subobjectsModel,
View, and Controlleris applicable in situations where the same data (Model) is
to be presented in different formats (Views), but is to be managed centrally by a

Filire d'ingnieur d'tat en Gnie logiciel


FSTS 2014/2015
single controlling entity (Controller). Pay special attention to the following words,
phrases, or terms appearing in the problem statement:

Separation of data presentation and data representation


Provide services to different clients: web client, WAP client, etc.
Multiple views, such as HTML or WML
Single controller

Вам также может понравиться