Talk about the MVC pattern

One: Introduction

(1) Introduction

The MVC pattern stands for the Model-View-Controller (Model-View-Controller) pattern. This pattern is used for layered development of applications.

  • Model (Model)  - Model represents an object or JAVA POJO for accessing data. It can also have logic to update the controller when the data changes.
  • View  - A view represents a visualization of the data contained by the model.
  • Controller (controller)  - Controller acts on the model and view. It controls the flow of data to model objects and updates views when data changes. It separates the view from the model.

(1) The top layer is the "view layer" (View) that directly faces the end user. It is the operation interface provided to the user and the shell of the program.

(2) The bottom layer is the core "data layer" (Model), which is the data or information that the program needs to operate.

(3) The middle layer is the "control layer" (Controller), which is responsible for selecting the data in the "data layer" according to the instructions input by the user from the "view layer", and then performing corresponding operations on it to produce the final result .

These three layers are closely related, but independent of each other, and changes in each layer do not affect other layers. Each layer provides an interface (Interface) for the upper layer to call. In this way, the software can be modularized, and it is not necessary to modify other layers to modify the appearance or change the data, which greatly facilitates maintenance and upgrades.

(2) Realization example

Create a Student object as a model   . StudentView  is a view class that outputs student details to the console, and StudentController  is a controller class responsible for storing data into  Student  objects and updating the view  StudentView accordingly .

The demo class uses  StudentController  to demonstrate the usage of the MVC pattern.

 

Two: advantages

1. Low coupling

By separating the view layer from the business layer, it is allowed to change the view layer code without having to recompile the model and controller code. Similarly, the change of an application's business process or business rules only needs to change the model layer (and controller) of MVC. Can. Because the model is separated from the controller and view, it is easy to change the data layer and business rules of the application.

The model layer is self-contained and separate from the controller and view layers, so it is easy to change the application's data layer and business rules. Once the model layer is implemented correctly, the view layer will display the data correctly regardless of whether the data comes from the database or the server. Since the three components of the application program using MVC are independent of each other, changing one of the components will not affect the other two, so a good low-coupling component can be constructed according to this design idea.

2. High reusability

As technology continues to advance, there are now more and more ways to access applications. The MVC pattern allows the use of various styles of views to access the same server-side code, thanks to the fact that multiple views can share a model.

For example, a user can order a certain product through a computer or a mobile phone. Although the ordering method is different, the way (process) to process the ordered product is the same. Since the data returned by the model is not formatted, the same component can be used by different interfaces (views). For example, a lot of data may be represented by HTML, but it is also possible to be represented by WAP, and the change of these representations only needs to change the implementation of the view layer, while the control layer and model layer do not need to make any changes.

Since the data and business rules have been separated from the presentation layer, code reuse can be maximized. In addition, the model layer also has the functions of state management and data persistence processing.

 

3. Low cost and good maintainability

The MVC pattern makes developing and maintaining user interfaces less technical. Separation of view layer and business logic layer makes WEB applications easier to maintain and modify.

4. Fast deployment, in line with software engineering requirements

Using the MVC pattern for software development reduces the software development time considerably. It allows back-end programmers to focus on business logic, and interface (front-end) programmers to focus on presentation.

Because different components perform their duties, different applications at each layer will have some common characteristics, which is conducive to managing program codes through engineering and tooling. At the same time, the controller also provides a benefit, that is, the controller can be used to connect different models and views to realize the needs of users, so that the controller can provide a powerful means for constructing applications.

Three: Disadvantages

1. Difficult to use

It's not easy to fully understand the MVC pattern. Using the MVC pattern requires careful planning, and because of its complex internals, it takes some time to think about the software's architecture. At the same time, due to the strict separation of the model and the view, it also brings certain difficulties to debug the application. Every component needs to be thoroughly tested before being used.

2. The system implementation is complex

For a simple interface, if you have to strictly follow the MVC pattern and separate the model, view and controller, it will increase the complexity of the structure, and may generate too many update operations, reducing operating efficiency.

3. Inefficient access to the view layer

The model operation interface may be different, and the view may need to call multiple times to obtain enough display data. Unnecessarily frequent access to unchanged data will also impair operational performance.

Note: If you access the model layer through the controller (rather than directly accessing the view layer), you may be able to avoid unnecessarily frequent access to unchanged data, thereby solving the problem.

Guess you like

Origin blog.csdn.net/m0_68988603/article/details/124854654