Beginners to learn Spring MVC framework - the first step, to understand the MVC pattern

What is the MVC pattern:

        One of the most important framework ideas - MVC

        The MVC design pattern is the Model View Controller design pattern, which specifies that the application includes a data model, presentation information, and control information. The pattern requires that each pattern be separated into distinct objects.

        Model (Model) - View (View) - Controller (Controller) [MVC] is an architectural pattern framework developed by Microsoft Corporation, which is used to replace ASP.NET in the form of a network for creating web applications. The MVC framework has the characteristics of a lightweight and testable framework, and divides the application into three components model, view and controller. These three layers have corresponding functions:

  • Model (Model): is the part of the application that is used to process the logic of the application data. Usually model objects are responsible for accessing data in the database.
  • View (view): is the part of the application that handles the display of data. Usually views are created from model data.
  • Controller (controller): is the part of the application that handles user interaction. Typically the controller is responsible for reading data from the view, controlling user input, and sending data to the model.

2585228e7f674fad88b91e8d3fd2e936.png

 

         The introduction of the MVC model has almost been completed. Let me explain the advantages and disadvantages of the MVC model.

Advantages and disadvantages of MVC

        Advantages of MVC:

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

        2. It is conducive to the division of labor and cooperation. In the MVC pattern, since the system is separated by layer, the division of labor in development can be better realized. Web designers can develop JSP in the view layer, those familiar with the business can develop the business layer, and other developers can develop the control layer.

        3. It is conducive to the reuse of components. For example, the control layer can be independently formed into a usable component, and the presentation layer can also be made into a common operation interface. Multiple views of a model can be created and used simultaneously at runtime.

        Disadvantages (deficiencies) of MVC:

        1. Increased the complexity of the system structure and implementation. For a simple interface, strictly follow MVC to separate the model, view and controller, which will increase the complexity of the structure, and may generate too many update operations, reducing operating efficiency.

        2. Too tight connection between view and controller. Views and controllers are separate but closely related components. Views have limited application without controllers, and vice versa, which prevents their independent reuse.

        3. View's inefficient access to model data. Depending on the model operation interface, the view may need multiple calls to obtain enough display data. Unnecessarily frequent access to unchanged data will also impair operational performance.

        4. At present, general advanced interface tools or constructors do not support patterns. The cost of adapting these tools to MVC needs and creating separate components is high, making MVC difficult to use.

 

Why do we use the MVC pattern?

        

        Most of the previous applications (non-Android applications) were created with procedural (object-oriented model has been fully supported since PHP5.0) languages ​​such as ASP, PHP or CFML. They mix data layer code like database queries with presentation layer code like HTML. More experienced developers will separate the data from the presentation layer, but this is usually not easy to do, it requires careful planning and constant experimentation. MVC basically enforces separating them. Although constructing an MVC application requires some extra work, the benefits it brings to us are undeniable.

        The most important point is that multiple views can share a model. Now more and more ways are needed to access the application, one of the solutions is to use MVC, then whether the user wants to use XML layout, or directly use code to write the interface or use HTML through the WebView control, use a Models can handle them. Since data and business rules have been separated from the presentation layer, code reuse can be maximized.

        Since the data returned by the model is not formatted, the same widget can be used by different interfaces. Models also have state management and data persistence capabilities.

        Because the model is self-contained and separated from the controller and view, it is easy to change the data layer and business rules of the application. If you want to migrate the database from MySQL to Oracle, or change the RDBMS-based data source to LDAP, you only need to change its model. Once the model is implemented correctly, the view will display its data correctly regardless of whether its data comes from a database or an LDAP server. We don't need to think about this at the moment, the SQLite database integrated in Android can already meet our needs well. Since the three components of the MVC application are independent of each other, changing one of them will not affect the other two, so you can construct a good loosely coupled component according to this design idea.

        For us, the controller also provides a benefit, that is, the controller can be used to connect different models and views to complete the user's needs, so that the controller can provide a powerful means for constructing applications. Given some reusable models and views, the controller can select the model to process according to the user's needs, and then select the view to display the processing results to the user.

 

 

Guess you like

Origin blog.csdn.net/m0_56417836/article/details/126648369