The MVC architecture is not a new design pattern that was invented to create Web sites. In fact, this architecture has been often discussed, if rarely implemented, since the early days of object-oriented programming. The concept that underlies the architecture is the simple divide-and-conquer approach. It seems to be human nature to create giant, monolithic programs that perform every task under the sun. Less obvious, but more useful is the concept of layers where a system is subdivided into smaller functional units. Each layer is given a specific, but limited, set of tasks to perform. This approach is not unlike the division of labor principle that became popular during the industrial revolution, where workers are given a specific job to perform, instead of having everyone do every job in the plant. In the MVC architecture, a system's tasks are subdivided into three layers: The Model All the data, as well as the code that stores and retrieves it, is located in this layer. The View All the processing that is concerned with appearance such as fonts, colors, and animation are handled by this layer. The Controller The rest of the code goes here. This layer normally contains the logic of a program. For example, a Web site's user interface might enable you to type a price of $5000 for a new car, and the database of current inventory might contain one of these cars. But the business rules state that a car cannot be listed for less than its cost, so the controller layer generates an error message that rejects the input on business rule grounds. The effect of this subdivision can be very positive on the behavior of your system: Performance Separating your system into three logical parts enables you to measure and tune the performance of each independently. In addition, you have the option to move one or more parts to a dedicated server if necessary. High Availability The separation of layers allows for unit testing to be performed. This results in fewer errors that occur during the initial days after going into production. Scalability The controller and model module can be a very complex piece of code. By separating out this piece, you have made it possible to implement it as servlets or EJBs, both of which scale nicely. Maintainability It is much easier to work on a small system than a large one. By dividing the system into three pieces, we gain an advantage when we go to make changes or to replace one piece. It is much easier to comprehend one logical part and make the changes correctly. Affordability It is easier to track the progress and make changes to three smaller systems than one giant one. Learnability The learning curve that must be traversed is cut into three parts, allowing a new person to be productive on one of the parts quickly. In addition, if the technology that was used to create one of the parts is superceded by a superior approach, that part can be replaced without much impact on the others. This reduces the chances that you will be stuck supporting a technology that the programmers hate to work on. The level of compliance to this model varies from technology to technology. Some of the approaches in this book provide a clear separation between layers, whereas others leave the task of separation into layers to the programmer. |