Model-View-Controller pattern (MVC pattern, one of the 10 common architectural patterns)

, Introduction:

  An architectural pattern is a generic, reusable solution to a recurring problem in software architecture in a given context. Architectural patterns are similar to software design patterns, but have a broader scope.

  The Model-View-Controller pattern, also known as the MVC pattern. It is a software architecture pattern in software engineering that divides a software system into three basic parts:

    Model (Model): responsible for storing the central data of the system.

    View (View): Display information to the user (multiple views can be defined).

    Controller (Controller): process the information entered by the user. Responsible for reading data from the view, controlling user input, and sending data to the model, it is the part of the application that handles user interaction. Responsible for managing interaction controls with users.

  Views and controllers together form the user interface.

  And each view has an associated controller component. The controller accepts input, usually as events encoding mouse movements, mouse button activity, or keyboard input. The time is translated into a model or attempted server request. The user interacts with the system only through the controller.

2. Structure

  The model component contains the functional core of the application, which encapsulates the corresponding data and outputs the process of performing specific application processing; the model also provides functions for accessing data, which are used by the view components to obtain the data to be displayed.

  The controller calls all these procedures on behalf of the user.

3. Purpose

  Realizing a dynamic program design is to simplify the modification and extension of the program later, and to make the reuse of a certain part of the program possible.

  By simplifying the complexity, the program structure is more intuitive.

  Separate the internal representation of information from how it is presented and accept user requests. It separates components and allows efficient code reuse. That is, the implementation code of the model and the view is separated, so that the same program can use different representations. For example, you can use a histogram and a pie chart to represent a batch of statistical data. The purpose of C is to ensure the synchronization of the model and the view. Once the model changes, the view should be updated synchronously.

4. Features

  An important feature of MVC is the separation of two types:

  Separation of view and data model: Use different views to display the same data; separate visible and invisible components, and independently test the model. Because the separation of visual components reduces external dependencies and facilitates testing. (A database is also an external component)

  Separation of view and presentation logic (Controller): Controller is a presentation logic component, not a business logic component. MVC can be used as a presentation mode or as a construction mode, which means that the Controller can also be a business logic. Separation of logic and specific display enables independent testing of logic.

5. The difference between the MVC architecture pattern and the layered pattern

  The MVC pattern belongs to the category of design patterns. Just like other design patterns, the appearance of the pattern is to optimize a certain function, and the MVC pattern can be regarded as a subdivision and optimization of the presentation layer in the three-tier architecture. In my understanding, the MVC pattern is the most commonly used architectural pattern for the presentation layer in the layered pattern.

  The details are as follows:

  Strictly speaking, the addition of these three is the presentation layer in the three-tier architecture. That is to say, MVC divides the UI layer in the three-tier architecture again into three parts: controller, view, and entity. Complete the page logic, and communicate with the interface layer through the entity; while the C layer directly talks to the business logic layer in the three layers. Three layers and MVC can coexist. The three layers are divided based on business logic, while MVC is divided based on pages.

  The layering mode of the three-tier architecture is a typical upper-lower relationship, and the upper layer depends on the lower layer. However, as a performance mode, MVC has no upper-lower relationship, but a mutual cooperative relationship. Even if MVC is regarded as an architectural pattern, it is not a layered pattern. MVC and the three-tier architecture are basically incomparable, and are technologies applied in different fields.

Six, the difference between the MVC architecture pattern and the MVC framework

  The MVC framework enforces the separation of application input, processing, and output. Applications using MVC are divided into three core components: Model, View, and Controller. They each handle their own tasks. The three core components it uses all come from the MVC pattern. It's just that they are made more independent from each other in the framework to handle their respective tasks. The most typical MVC is JSP+SERVLET+JAVABEAN mode.

  In the MCV framework:

  View: The view is the interface that the user sees and interacts with. The main elements of the view are HTML, Adobe Flash, XHTML, XML/XSL, WML and some other markup languages ​​and Web services.

  Model: Model data and business rules.

  Controller: The controller accepts the user's input and calls the model and view to complete the user's needs, so when clicking a hyperlink in a Web page and sending an HTML form, the controller itself does not output anything or do any processing. It just receives the request and decides which model component to call to process the request, and then decides which view to use to display the returned data.

7. Examples of MVC Architecture Patterns

  For example, ①, the kind of cassette game consoles I played when I was a child, Control is the host. Generally speaking, I just buy a host. As long as it is not broken, he can let me play this type of game all the time. View is a TV and a game controller. The TV can work independently. It doesn’t matter whether the input is a TV signal, a DVD player signal or a game console signal. It just displays, and it decides what we see. I want a larger size or a color display, I just need to buy a corresponding TV, the handle can also be replaced, and the joystick is still with vibration. The Model is the game cassette, which determines what game I am playing, whether it is Contra or Super Mario, and the game console and TV manufacturers will never know what kind of game may be run on it. There may be game codes and storage units in the cartridge, which are designed according to the needs of the game.

  For example ②, a simple information system for political elections using scale representation, which provides a spreadsheet of input data and several graphs representing the current results. Users can interact with the system through a graphical interface. All information displays must immediately reflect changes in election data. (Quoted from "Pattern-Oriented Software Architecture - Volume 1 Pattern System")

  That is, once the model's data has changed, the model should notify all views.

Guess you like

Origin blog.csdn.net/lmrylll/article/details/131272753