Anyone who knows me will tell you I am a big fan of organization and order, especially when it comes to sensible code management, which is why I really like this pattern. I look at the MVC as more of a way to structure and organize certain objects of a code base in an application; whether it is a web or desktop application, the same idea applies. Keeping the core server logic separated from the view or the data that the user is interacting with by adding a central layer, called the controller, not only keeps our code organized, it also creates a logical data flow that is easier to manage. Believe it or not, in theory this pattern exists in every web applicationit is simply not always organized into separate layers or there are variations of organization. Take a look at Figure 15.1, which shows the MVC pattern as it applies to our Ajax application.
Figure 15.1. The MVC pattern applied to the sample project.
After taking a look at Figure 15.1, it is obvious this pattern exists in all applications. Even if there is only one code file in an application, the underlying data flow is always present. For example, all applications must make decisions based on user interaction and interact with the server by storing or retrieving datawhether it is attached to a database or simply making HTTP requestsand respond to the user with new data. The controller would be the decision making, the model would be the server interaction, and the view would be the response to the user. In theory, the XHR is exclusively an MVC pattern because it takes user input, interacts with the server via the HTTP request, and responds to the user via the responseText and responseXML properties. The difference is in the structure and design, which are definitely debatable and open to interpretation. Reusable objects are the ultimate goal of any application, and decoupling the objects is what makes this pattern powerful. Therefore, this pattern may exist in all code in theory, but it is not until we pair it with an object-oriented and structured design that its true power is unleashed.