How does MVC work?

Posted September 3, 2008 by kanian77
Categories: MVC and the web


When the user interacts with an application designed using the MVC principles, the Model, the View, and the Controller communicate together to give the user the ability to interact with the Model and perceive the expected results.

There are many ways in which the Model, the View and the Controller can work together as explained by Trygve Reenskaug in his paper.

III.1 The passive model

Here the Model reacts only to the Controller’s requests and View’s requests (if any). That is the Model would modify or retrieve data it represents only on the initiative of the Controller or the View. So upon the user’s request the controller would transmit the command to the Model and notify the View of a change in the Model’s state. The View could then request the changes to the Model or the Controller could trasmit those changes to the View. The Model is thus passive.

III.2 The active model

If the data represented by the Model can change as a result of actions from objects other than its View or Controller, then there must be a way for the Model to notify its View of the change. A communication channel must exist between the Model and its View. In such case, the Model must initiate communication with the View. The Model is said to be active.

III.3 The communication between the View and the Controller

As explained in Steve Burbeck 1992 paper on MVC and Smalltalk:

Each view is associated with a unique controller and vice versa. Instance variables in each maintain this tight coupling. A view’s instance variable controller points at its controller, and a controller’s instance variable view points at its associated view. And, because both must communicate with their model, each has an instance variable model which points to the model object. So, although the model is limited to sending self changed:, both the view and the controller can send messages directly to each other and to their model.

In this excerpt ’self changed’ is the message that the active model sends to its views to tell them that its state has been modified.

———-

Note:

The tight coupling of the view and the controller have pushed some implementers of the MVC pattern in the GUI world to combine the view and the controller into one element called the View, whereas the model is called the Document. Such a pattern is then called the Document View pattern1

———–

1 Model View Controller explication page of the phpWACT framework, http://www.phpwact.org/pattern/model_view_controller

MVC and Object Orientation

Posted September 3, 2008 by kanian77
Categories: MVC and the web

II. MVC and Object Orientation

From what we read so far we understand that MVC separates the presentation of data, the handling of data, and the intepretation of user actions into different layers. One of the stated benefits by Trygve Reenskaug is the fact that data could be presented from mutiple perspectives. This means that, since the presentation (View) and the description of the data (Model) are embodied into different loosely coupled layers or components, the same data can be presented to the user in many different ways. That is, the data is not tied to the way it is being shown to the user. Moreover, let’s read this excerpt from Steve Burbeck’s text:

The formal separation of these three tasks is an important notion that is particularly suited to Smalltalk-80 where the basic behavior can be embodied in abstract objects: View, Controller, Model and Object. The MVC behavior is then inherited, added to, and modified as necessary to provide a flexible and powerful system.

We see how MVC is well suited to be implemented using object oriented techniques. As a matter of facts, Trygve Reenskaugh in his Models-Views-controllers note writes on models:

The models are represented in the computer as a collection of data together with the methods

necessary to process these data.

Now if this is not the description of an object, then what is it? Now if models can be understood as objects in MVC, why not think of Views and Controller as other classes of objects? The fact is that a lot of systems using the MVC pattern also use the object oriented pattern. How well do both patterns fit together and how to better use them together? These are the questions that the developper of such systems and frameworks have probably tried to answer. Let us just remember that both patterns have been applied successfully in Smalltalk-80, with the object orientation supporting the MVC pattern.