Wednesday, November 14, 2007

The MVC Pattern

The Model-View-Controller pattern is the most commonly used pattern for software development, after the N-tiered model. In the MVC pattern, the model is responsible for managing data. It's state is accessed by the view and mutated by the controller. The sole function of the view is to display data to the user. The controller is responsible for reacting to triggers from the user, for changing the state of the model, and for updating the view. Both the controller and the view are dependent on the model; however, the model is independent of both the controller and the view.

There are two variants of the MVC - the passive model, in which the model does not notify the view of any changes to it's state, and the active model, in which the model notifies the view of changes to it's state usually via the observer pattern.

The 3-tier architecture differs from the MVC in that there is a strict separation between the three layers, which prevents the top layer from communicating with the bottom layer. In the MVC, the model can notify the view of changes to it's state.

A pretty good diagram of the MVC can be found here:
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/images/app-archa2.gif

No comments: