MVC: what it is and how it relates to the user interface
Recently, I talked a lot about MVC ( Model-View-Controller, “model-view-controller”) with a number of programmers who hold positions at various levels in software companies. I met a lot of misunderstanding and now I want to share my thoughts on this issue. MVC is a very abstract thing, and this is the main cause of misunderstanding – an underestimated degree of abstractness. It manifests itself in the following: a programmer, as a rule, works within the framework of one paradigm (OOP our everything) and looks at the world through it. And when he hears, for example, “controller” – represents the class. And this particularity is a class, or not a class, or a collection of objects or procedures, it does not matter. The understanding is also distorted by the specific MVC framework. I’ll try to influence it.
What is MVC?
So, MVC is about the user interface (UI). Not necessarily graphic, voice control is also good. Do not forget that a program may not have a user interface, it may have a program interface (API) or it may not have any at all and still be useful.
But if we have a user, then there must be a user interface. What is the interface? This is the adjacent border between the two systems. In our case: on the one hand – the program, on the other – the user. Here they are.
The program is completely abstract, any subject code. She knows how to do something useful, and the user has needs that can be met with this program. Then there are pieces of logic that “know” how, using this program, directly do what the user wants. Pieces – not subject, subject logic in the program. They relate more to the user with his specific needs and are combinations of calls and program calls.
MVC concept or what it consists of
The MVC model includes three components: Model, View and Controller.
The most important thing of course here is the first – this is Model. The model is a set of procedures and data processing algorithms. The model itself does not contain data, but, as a rule, it derives it from the database and processes it using predefined algorithms. If we talk about Web development, the model will contain a set of classes and functions, for example in PHP.
The second element is the View view. Allows you to display information. If this is a site, then the information is displayed in the browser. The representation when developing websites contains HTML code in which variables are inserted, which are taken, no, not from the model, but from the controller.
So the third element is the Controller. Its main function is to provide communication in the user and model. May also contain PHP code.
Many novice developers do not use the Model or use it only to access the database, which is the main mistake, incorrectly and contrary to the logic of the MVC model. And all the main code is placed in the controller. The consequences of this is, firstly, a huge code, and secondly, a lower application speed. Well, to make changes to the code of such applications is much more difficult.
For example, consider the MVC model for a news site.
The user enters the site page. It displays a default page with a specific list of news. Information for the formation of news is taken from the database.
When a user has typed a specific page in the address bar of the browser, the request is transmitted to the controller, and the function that processes it and loads the Model is launched.
The model, having received the necessary data already processed and grouped into an object or an array, will generate a query to the database. After receiving the data, the Model forms them in a certain form and sends them to the Controller, and then transferred to the View. It will not be an error if the data is transferred immediately to the View. But again, extra actions are used, which eventually lead to a longer loading of the site and applications.
This model is used in many control systems and frameworks, one of which is CodeIgniter, which recently acquired a second life.
Thus, using the MVC model, you can easily create an administration system for the site, an Internet application. So the CodeIgniter framework uses this particular model.
Let’s complete our diagram with a place for use cases.
These can be buttons and other graphical input/output elements, gestures, speech recognition, and synthesis. Any version of the exchange of data and commands. Voila:
The user pulls one of the user cases, which, in turn, manipulates the program. The program publishes the result or changes in its condition.
So where is MVC used?
All that is left is to only distribute familiar names to the resulting components.
When a model publishes changes, it doesn’t care for anyone, it doesn’t know anything about View. Instead of or together with View, there may be another subsystem on that end.
Now a few particulars.
It was a classic version of MVC – Active Model. It also happens that the model does not notify about changes. Then the controller takes on this responsibility. He knows what manipulations he makes on the model, and, obviously, he knows what changes in the state of the model can follow. This is the Passive Model.
And one moment. The division of the code into objective and non-objective is conditional and depends on how pedantically we want to model the subject area. Sometimes it is a rational decision – to include some sort of code in the model. Perhaps this will reduce the amount of code as a whole and simplify it.