Monday, May 17, 2010

Model View Controller

The model view controller is a compound pattern. Its three parts are described as:

Model: This is the business logic of the application - things like its data, its underlying rules, things like that.

View: A view is a representation of the data to the user. For example, it could be a graphical widget that shows the current data, and updates itself when the data changes.

Controller: Controller deals with user input and handles the complexity of decision making on how to transform user actions into operations on the model.

So, what design patterns are we talking about?

- First off, the interface between the model and its clients (both view and controller) follows the Observer pattern. Both views and controllers register as observers on the model. This way when data changes, the view can update itself automatically. Also, the controller can transform the view based on certain things in the model changing.

- Second design pattern is Strategy. The controller is a strategy, that the view is composed with. A view can be composed with a different controller to get different behavior.

- Third design pattern is Composite, used by the view in its internal implementation. Typically view is a tree of menus, controls, etc. By telling the top level control to draw itself, the view can make the entire UI draw itself (for example) by using the Composite pattern.

No comments: