HFDP - Model View Controller (MVC)

MVC is probably the most known compound pattern, since it describes naturally almost any modern application.

Let's think for instance to an mp3 player. The user sees on the View what is the current status of the player. Using the interface, the user asks the application to do something new, as playing another song, and the request is passed to the Controller. The Controller acts consequently, asking the model to change its status. The View observes the Model, and reacts changing accordingly when notified by the Model.

Model: keeps all the data, the state and the application logic.
View: gives the user a presentation of the model.
Controller: receives the user inputs and asks for changes in the Model and View accordingly.

The user has only access to the View. His requests are passed to the Controller.

The Controller translates the request it gets from the View in calls to the Model.

If required, the Controller could also ask the View to change.

The Model notifies the View when its state changes. And the View could ask the Model for more data to integrate the information to be showed to the user.

The Observer Pattern connects the Model to the View. The Model is observed by the View.
The Strategy Pattern connects the View to the Controller. The Controller implements the behaviour of the View.
The Composite Pattern is used to structure the View, to manage all its components.

More on MVC, and on compound patterns, in chapter twelve of Head First Design Patterns, by Freeman, Freeman, Sierra, Bates.

No comments:

Post a Comment