The difference and understanding between MVC and MVVM

MVC

In the classic MVC pattern, M refers to the business model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different forms of expression.

MVC programming model

M (Model): Model layer. It is the part of the application program used to process application data logic, and the model object is responsible for accessing data in the database;
V (View): View layer. It is the part of the application that deals with data display, and the view is created based on the model data;
C (Controller): the control layer. It is the part of the application that handles user interaction. The controller accepts user input and calls the model and view to complete the user's needs. 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.

 Advantages and disadvantages of MVC

advantage

1. Low coupling

The view layer is separated from the business layer, which allows changing the view layer code without recompiling the model and controller code. Similarly, changes to an application's business process or business rules only need to change the MVC model layer. Because the model is separated from the controller and view, it is easy to change the data layer and business rules of the application.

2. High reusability

The MVC pattern allows the use of various styles of views to access the same server-side code, because multiple views can share a model, which includes any WEB (HTTP) browser or wireless browser (wap), for example, the user can pass through the computer It is also possible to order a certain product through a mobile phone. Although the ordering method is different, the way to process the ordered product is the same. Since the data returned by the model is not formatted, the same widget can be used by different interfaces.

3. Fast deployment and low life cycle cost

MVC reduces the technical content of developing and maintaining user interfaces. The use of MVC mode can greatly reduce the development time. It allows programmers (Java developers) to concentrate on business logic, and interface programmers (HTML and JSP developers) to concentrate on presentation.

4. High maintainability

Separating the view layer and business logic layer also makes WEB applications easier to maintain and modify.

shortcoming

1. Fully understanding MVC is more complicated.

Since the MVC model has not been proposed for a long time, and the students have insufficient practical experience, it is not an easy process to fully understand and master MVC.

2. Debugging is difficult.

Because the model and view should be strictly separated, it also brings some difficulties to debug the application, and each component needs to be thoroughly tested before it can be used.

3. Not suitable for small, medium-sized applications

In a small and medium-sized application, mandatory use of MVC for development often takes a lot of time, does not reflect the advantages of MVC, and makes development cumbersome.

4. Increase the complexity of 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.

5. The connection between the view and the controller is too tight and reduces the access of the view to the model data

The view and the controller are separated from each other, but they are closely related components. The view has no controller, and its application is very limited, and vice versa, which prevents their independent reuse.

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.

MVVM

MVVM is shorthand for Model-View-ViewModel. It's essentially an improved version of MVC.

MVVM includes view view layer, model data layer, viewmodel layer. Using two-way data binding, changes in the View are automatically reflected in the ViewModel, and vice versa.

MVVM is to abstract the state and behavior of the View in it, and let us separate the view UI from the business logic. Among them, ViewModel is the glue between View and Model layer, which can take out the data of Model and help to deal with the business logic involved in View due to the need to display content. To put it bluntly, it is to separate the business logic and page logic of the original ViewController layer into the ViewModel layer

M (Model): Model layer. It is the data object related to business logic, which is usually mapped from the database. We can say that it is the model corresponding to the database.
V (View): View layer. It is the displayed user interface.
VM (ViewModel): View model layer. A bridge connecting view and model. Because the data in the Model layer often cannot directly correspond to the controls in the View one by one, so it is necessary to define a data object that specifically corresponds to the controls on the View. The responsibility of ViewModel is to encapsulate the model object into an interface data object that can display and accept input.

Advantages of MVVM

The MVVM pattern is the same as the MVC pattern. The main purpose is to separate the view (View) and the model (Model). There are several advantages

1. Low coupling . The View (View) can be changed and modified independently of the Model. A ViewModel can be bound to different "Views". When the View changes, the Model can remain unchanged, and when the Model changes, the View can also remain unchanged.

2. Reusability . You can put some view logic in a ViewModel and let many views reuse this view logic.

3. Independently developed . Developers can focus on business logic and data development (ViewModel), designers can focus on page design, and use Expression Blend to easily design interfaces and generate xaml codes.

4. Testable . The interface has always been more difficult to test, and the test can be written for the ViewModel.

The difference between MVC and MVVM

In MVC, View can directly access Model, so View will contain Model information and some business logic. The MVC model focuses on the invariance of the Model, so in the MVC model, the Model does not depend on the View, but the View depends on the Model. Not only that, because some business logic is implemented in View, it is difficult to change View, at least those business logic cannot be reused.

Conceptually, MVVM is a mode that really separates the page from the data logic. It implements the data binding work in a JS file, and the main function of this JS file is to complete the data binding, that is, to bind the model to the UI. on the elements. In addition, another important feature of MVVM is two-way binding , which makes it easier for you to maintain N areas on the page that depend on a certain field at the same time, without having to update them manually.

Both MVC and MVVM are a kind of design thinking. The main reason is that the Controller in MVC evolves into the viewModel in MVVM. MVVM mainly solves the problem that a large number of DOM operations in MVC reduce the page rendering performance and slow down the loading speed.

The biggest difference between MVVM and MVC is that it realizes the automatic synchronization of View and Model: when the properties of Model change, we no longer need to manually manipulate the Dom elements to change the display of View, it will change automatically.

Guess you like

Origin blog.csdn.net/m0_53206841/article/details/125824684