Servlet-based MVC design pattern

1.   The distinction between the functions of various components of MVC

Each component and technology has its own functions and characteristics. When writing programs, we should design their functions based on their functions. It is like eating in a restaurant, the waiter provides the recipe to the customer, and the customer orders according to the recipe. , and then hand the menu to the waiter, and the waiter will hand it over to different chefs according to the different cold and hot dishes in the menu.

analyze:

The waiter is the organizer and controller (Controller) of this process. He is responsible for receiving customers, displaying the recipes to customers, and handing the customer's order content (similar to the user's request) to the chef to process the dishes (similar to visiting Database and Java classes that handle business), and finally the waiter brings the dishes to the customer (similar to responding to JSP).

In this process, the first thing customers see is the recipe, and then the whole table of dishes. In the program, what the user can see is the HTML, JSP page, this part is called the view (View). When the waiter gives the customer's order to the chef, the chef uses different raw materials and ingredients to process the dishes according to different dishes. This is similar to submitting different request data according to the user in the program, accessing the database or performing business logic processing, this part is called the model (Model).

In program design, the design method using Model, View, and Controller is called the MVC design pattern.

2.   What is a design pattern

A design pattern is a set of repeated, successful code design experiences. Patterns must be solutions to typical problems (not individual problems). For example: my country's "Constitution" stipulates that as long as the act of robbery constitutes the crime of robbery, rather than defining the crime of robbery for a specific case, for example, robbery of 1 yuan constitutes the crime of robbery of 1 yuan.

Design patterns provide solutions for a certain type of problems, and at the same time, design patterns optimize the code, making the code easier for others to understand, improving reusability, and ensuring the reliability of the code.

Each design pattern describes a problem that constantly arises in our environment, and then describes the core of the solution to that problem. In this way, you can use those existing solutions countless times without repeating the same work.

3.   MVC design pattern

The MVC design pattern consists of the following three parts:

Model (Model): The corresponding component is a JavaBean (Java class).

View (View): The corresponding component is a JSP or HTML file.

Controller (Controller): The corresponding component is Servlet.

Models can be divided into business logic and data models, which represent the business logic and state of the application.

Views provide an interactive client interface that displays model data to clients.

The controller responds to the client's request, operates the model according to the client's request, and displays the model's response result to the client from the view.

4.   Advantages and benefits of the MVC design model

(1), perform their own duties without interfering with each other

In the MVC pattern, the three layers perform their own duties, so if the requirements of any layer change, you only need to change the code in the corresponding layer without affecting other layers.

       (2), is conducive to the division of labor in the development

In the MVC pattern, since the system is separated by layers, the division of labor in development can be better realized. Web page designers can develop JSP pages, developers who are familiar with the business can develop methods for related business processing in the model, and other developers can develop controllers for program control.

       (3), is conducive to the reuse of components

Layering is more conducive to the reuse of components. For example, the control layer can be independently formed into a general component, and the view layer can also be formed into a general operation interface.

       The most important feature of MVC is the separation of display and data, which increases the reusability of each module.

5.   MVC programming mode

The servlet receives the client request, calls the corresponding model to process the business logic and data, and then the servlet selects the corresponding JSP or HTML file to respond to the client according to the processing result.

6.   In the JavaEE three-tier architecture, the middle layer corresponds to the two modules of the controller and the model in the MVC design pattern.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769959&siteId=291194637