MVC is very important in web development. This page was created to provide a brief overview of MVC. Note that if you will be using Zend or any other major web application framework, you do not need to know how to implement MVC but only what it is.
MVC, short for Model View Controller, was invented at Xerox in the 1970s. It has had profound impact on web development and GUI software development in general. Many variant implementations of MVC exist and it is easy to find to bloggers quarreling over what MVC is. Here MVC is presented from design point-of-view rather than coding point-of-view. Code should follow the model and design, not the other way around. Once MVC is seen from design point-of-view, there isn't much room left for debate.
MVC design pattern or architecture pattern?
MVC is an architecture pattern according to Pattern-Oriented Software Architecture by Buschmann published in 1996. Design Patterns by the gang of four (Gamma et al) which introduced design patterns to the computer science community does not list MVC as a design pattern. Instead it talks about how to use design patterns code for MVC. Even though, MVC is often referred to as a design pattern, it is actually an architecture pattern and you can use several different design patterns to code for MVC. More on this later on this page.
What is MVC?
MVC is divides an application into three components: Model, View, and Controller.
- Model is application object. It contains data and logic portion of the software.
- View is screen presentation. It renders the Model to the screen.
- Controller handles user interaction and provides user interface tools
A subscribe/notify protocol needs to be established between view and model. View (presentation) must represent the state of the model. Whenever, model state changes, the view is notified and it updates itself. The beauty of this solution is that new views can be developed without modifying the model.
Suppose you visit Google. The page with the form you see is the created by the View component of the software. When you type and click, your input is handled by the Controller and directed to the Model. Based on you input, Model queries the Google database, brings a list of sites, advertisements, etc. View is notified of this change in state of Model and it updates the page to show the content. I am not saying that this is how Google works or that it even uses MVC but if it is created with MVC, it would roughly resemble this.
MVC and design patterns
Following is a vision of how design patterns could be applied to MVC as seen by writers of the landmark book, Design Patterns.
Problem: When a Model object changes state, a View object needs to be notified.
This can easily be handled by the Observer design pattern. It observes Model and notifies View whenever its state changes.
Problem: A View can composed of several view objects, e.g. a widget containing several GUI components. We often want to group all View objects (widget components) and treat them as one View object.
This can be easily handle with the Composite design pattern. It allows for development of class hierarchy which could then be manipulated as one class object.
Problem: Changing the way a view responds to user input without changing its presentation.
This type of View-Controller relationship can be modeled with the Strategy design pattern.
MVC and web application frameworks
Implementing MVC can be tricky unless you really understand its subtle complexity. Generally, you learn this by stumbling over blocks while coding MVC. Luckily, most web application frameworks provide canned solutions for MVC implementation so you don't need to learn to design and program it. In fact, I strongly recommend that you use a framework instead of trying to re-invent the wheel. Zend provides MVC functionality.