This material is part of the “Introduction to Enterprise Development” series. Previous articles:
- about the network ;
- about software architecture ;
- about HTTP/HTTPS protocols ;
- about Maven basics ;
- about servlets (we are writing a simple web application) ;
- about servlet containers .
History of MVC
The ideas for MVC were formulated by Trygve Reenskaug while working at Xerox PARC in the late 70s. In those days, to work with a computer it was impossible to do without an academic degree and constant study of voluminous documentation. The problem that Reenskaug solved together with a group of very strong developers was to simplify the interaction of the average user with a computer. It was necessary to create tools that, on the one hand, would be extremely simple and understandable, and on the other, would make it possible to manage a computer and complex applications. Reenskaug worked on the team that developed the portable computer "for children of all ages" - Dynabook, as well as the SmallTalk language under the leadership of Alan Kay. It was then and there that the concepts of a friendly interface were laid down. Reenskaug's work with his team greatly influenced the development of the IT field. Let us present an interesting fact that does not relate directly to MVC, but illustrates the significance of those developments. In 2007, after the presentation of the Apple iPhone, Alan Kay said: “When the Macintosh came out, Newsweek asked what I thought of it. I said: this is the first personal computer worthy of criticism. After the presentation, Steve Jobs came up and asked: is the iPhone worthy of criticism? And I said, make it five by eight inches and you will conquer the world.” Three years later, on January 27, 2010, Apple introduced the 9.7-inch iPad. That is, Steve Jobs followed Alan Kay's advice almost literally. The project that Rennskaug worked on lasted for 10 years. And the first publication about MVC from its creators was published another 10 years later. Martin Fowler, author of a number of books and articles on software architecture, mentions that he learned MVC from a working version of SmallTalk. Since there was no information about MVC from the primary source for a long time, as well as for a number of other reasons, a large number of different interpretations of this concept have appeared. As a result, many people consider MVC to be a design scheme or pattern. Less commonly, MVC is called a composite pattern or a combination of several patterns that work together to implement complex applications. But in fact, as said earlier, MVC is primarily a set of architectural ideas/principles/approaches that can be implemented in various ways using various patterns... Next, we will try to look at the main ideas embedded in the MVC concept.What is 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 acronym that stands for Model-View-Controller.
Step 1: Separate the application's business logic from the user interface
The key idea of MVC is that any application with a user interface, as a first approximation, can be divided into 2 modules: a module responsible for implementing the business logic of the application, and a user interface. The first module will implement the main functionality of the application. This module will be the core of the system, in which the application domain model is implemented. In the MVC concept, this module will be our letter M, i.e. model. The second module will implement the entire user interface, including displaying data to the user and the logic of user interaction with the application. The main purpose of this separation is to ensure that the core of the system (Model in MVC terminology) can be independently developed and tested. The application architecture after such a division will look like this:Step 2. Using the Observer pattern, achieve even greater independence of the model, as well as synchronization of user interfaces
Here we pursue 2 goals:- Achieve even greater independence of the model.
- Synchronize user interfaces.
Step 3. Dividing the interface into View and Controller
We continue to divide the application into modules, but at a lower level of the hierarchy. In this step, the user interface (which was separated into a separate module in step 1) is divided into a view and a controller. It's difficult to draw a strict line between a view and a controller. If we say that the view is what the user sees, and the controller is the mechanism through which the user can interact with the system, there is some contradiction. Controls, such as buttons on a web page or a virtual keyboard on a phone screen, are essentially part of a controller. But they are just as visible to the user as any part of the view. Here we are talking more about functional division. The main task of the user interface is to ensure user interaction with the system. This means that the interface has only 2 functions:- display and conveniently display information about the system to the user;
- enter user data and commands into the system (transmit them to the system);
- Following the principles of MVC, the system needs to be divided into modules.
- The most important and independent module should be the model.
- The model is the core of the system. You need the ability to develop and test it independently of the interface.
- To do this, at the first step of system segregation, you need to divide it into a model and an interface.
- Next, using the Observer pattern, we strengthen the model in its independence and obtain synchronization of user interfaces.
- The third step is to divide the interface into a controller and a view.
- All that is required to enter information from the user into the system is into the controller.
- All that outputs information from the system to the user is in view.
GO TO FULL VERSION