MVC architecture (interview)

1 Overview

The full name of MVC is Model View Controller (Model View Controller), which is a design paradigm that separates business logic, data, and interface display to write code. Gather business logic into one component, and you do not need to change the background logic while improving page effects and user interaction (to put it bluntly, changing the front-end code).

2. The meaning of each part

Model (Business Model): The bottom layer is the core "data layer", which is the data or information that the program needs to manipulate. (Understanding: Except for V and C, all others are M)

View: The view layer directly facing the user is the operation interface provided to the user and the shell of the program (understand: all that can interact with the user are V, including the place where user input is required in the console, including java development The interface of the desktop application also belongs to V).

Controller (controller): The middle layer is responsible for selecting the data of the Model layer according to the instructions input by the user in the View layer, and then performing corresponding operations on it, and producing the final result (understanding: the view sends a request to the controller, The controller selects the model to process the data, the result returned by the model is passed to the controller, and the controller selects the corresponding view).

According to the concept of these three layers, we can know that the MVC architecture combines codes with the same function to form "layers", and separates the codes with different functions to form different "layers". The layers are independent of each other and do not interfere with each other. , Only for information exchange. According to the concepts between the layers, the advantages and disadvantages of the MVC architecture can be deduced.

3. Advantages of MVC architecture

① Each code performs its duties without interfering with each other (low coupling)

The layers are separated, and when changing the code logic of a certain layer, there is no need to change the code logic of other layers. For example, if the page style of the view layer is changed, there is no need to change the background logic.

②High reusability

The MVC development model allows multiple views of different styles to access the code of the same server. For example, an e-commerce application has a web interface and a mobile interface. In the same function-all are added to the shopping cart, the front-end interface is different, but the back-end logic is the same, then the back-end logic code (that is, M and V ) Can use the same set of codes.

③Reduce development cycle and improve quality

Using the MVC model enables front-end personnel to focus on the development of front-end, and back-end personnel to focus on writing back-end code. The division of labor is clear and each performs its own duties, which can reduce development states and improve code quality.

④High maintenance

Separate between the layers, where there are problems and where to modify. For example, there is a problem with the business logic, and M needs to be modified, and the other two layers of code do not need to be changed. If these logics are all written together, it is not only inconvenient to find, but also inconvenient to modify.

4. Disadvantages of MVC

①High complexity

For applications with relatively simple business, the use of MVC framework pattern is a bit outweigh the gains. (Understanding: For example, most of the view layer only needs to simply query the data in the database, without any processing of the data, so that the service layer is added between the view layer and the data layer, as opposed to directly calling the view layer For the data layer, the amount of code is increased, and more instantiated objects are generated. Development efficiency is reduced, and operation efficiency is reduced).

②The view's access to model data is inefficient

Adopting the MVC framework model will cause the view layer to access the data layer and get the data, which requires many methods to be called, which will reduce operating efficiency.

5. Frame pattern and design pattern

MVC is a framework pattern.
The framework pattern and the design pattern are difficult to distinguish between the two. The framework pays more attention to the reusability of the code, and the design pattern is the design reuse. The architecture is somewhere in between, part code reuse, part design reuse, and part analysis reuse. Design pattern is the knowledge of software and cannot be expressed directly in code. It is a design idea. Only examples of design pattern can be expressed in code. There is no MVC design pattern in our 23 common design patterns (as shown in the figure below).
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44613100/article/details/107984059