This material is part of the "Introduction to Enterprise Development" series. Previous articles:
- about networking
- about software architecture
- about HTTP/HTTPS
- about the basics of Maven
- about servlets (writing a simple web application)
- about servlet containers
History of MVCThe ideas behind MVC were formulated by Trygve Reenskaug while working at Xerox PARC in the late 1970s. In those days, working with computers required a degree and constant study of voluminous documentation. The task solved by Reenskaug together with a group of very strong developers was to simplify an ordinary user's interaction with the computer. It was necessary to create tools that, on the one hand, would be extremely simple and understandable, and on the other hand, would make it possible to control computers and complex applications. Reenskaug worked on a team that developed a laptop computer "for children of all ages" — the Dynabook, as well as the SmallTalk language under the leadership of Alan Kay. That was when the concepts of a friendly interface were laid down. In many respects, the work done by Reenskaug and his team influenced the evolution of the IT sphere. Here is an interesting fact that doesn't apply to MVC directly, but illustrates the significance of these developments. Alan Kay said, "When I first got to Apple, which was in ’84, the Mac was already out and Newsweek contacted me and asked me what I thought of the Mac. I said, 'Well, the Mac is the first personal computer good enough to be criticized.' So, after announced the iPhone in 2007, he brought it up to me and handed it to me. He said, 'Alan, is this good enough to be criticized?' And I said, 'Steve, make it this size as big as a tablet and you’ll rule the world.'" After 3 years, on January 27, 2010, Apple introduced the iPad with a diagonal of 9.7 inches. In other words, Steve Jobs followed Alan Kay's advice almost exactly. Reenskaug's project lasted for 10 years. But the first publication about MVC came to light after another 10 years. Martin Fowler, author of several books and articles on software architecture, mentions that he studied MVC using a working version of Smalltalk. Because there was no information about MVC from the original source for a long time, and for several other reasons, a large number of different interpretations of this concept appeared. As a result, many consider MVC to be a design pattern. Less commonly, MVC is called a composite pattern or a combination of several patterns that work together to create complex applications. But, as mentioned earlier, MVC is actually primarily a set of architectural ideas/principles/approaches that can be implemented in various ways using different patterns... Next, we'll consider the main ideas embedded in the MVC concept.
MVC: Basic ideas and principles
- VC is a set of architectural ideas and principles for building complex information systems with a user interface
- MVC is an abbreviation that stands for: Model-View-Controller
Step 1. Separate the application's business logic from the user interfaceThe main idea of MVC is that any application with a user interface can be divided into 2 modules: a module responsible for implementing the business logic, and the user interface. The first module will implement the application's main functionality. This module is the core of the system, where the application's domain model is implemented. In the MVC paradigm, this module is the letter M, i.e. the model. The second module implements the entire user interface, including the logic to display data to the user and handle user interaction with the application. The main goal of this separation is to ensure that the core of the system (the "model" in MVC terminology) can be independently developed and tested. After making this separation, the application's architecture looks like this:
Step 2 Use the observer pattern to make the model even more independent and to synchronize user interfacesHere we have 2 goals:
- Achieve even greater independence for the model
- Synchronize user interfaces
Step 3 Separate the interface into view and controllerWe continue dividing the application into modules, but now at a lower level in the hierarchy. At this step, the user interface (which we separated into a distinct module in step 1) is split into a view and a controller. Drawing a strict line between the view and the controller is difficult. If we say that the view is what the user sees, and the controller is the mechanism that allows the user to interact with the system, you might point out a contradiction. Control elements, such as buttons on a web page or a virtual keyboard on a phone's screen, are basically part of the controller. But they are as visible to the user as any part of the view. What we're really talking about here is functional separation. The user interface's main task is to facilitate the user's interaction with the system. This means that the interface has only 2 functions:
- output and conveniently display system information to the user
- enter user data and commands (communicate them to the system)
- According to the principles of the MVC paradigm, a system must be divided into modules.
- The most important and independent module should be the model.
- The model is the core of the system. It should be possible to develop and test it independently from the user interface.
- To achieve this, in the first step of division, we need to split the system into a model and user interface.
- Then, using the observer pattern, we bolster the model's independence and synchronize user interfaces.
- The third step is to divide the user interface into a controller and view.
- All that is required to receiving user data into the system is in the controller.
- All that is required for delivering information to the user is in the view.