What is MVVM?
View and model under the MVVM framework can’t direct communication, only through the ViewModel interact, it is able to monitor the change of the data, and then notify the view for automatic updates, and when a user operation view, VM can also listen to the change of view, and then inform the corresponding data changes, this is actually the two-way data binding. And V and VM can communicate.
Model = Model
A model is either a domain model that represents real state content (object-oriented) or a data access layer that represents content (data-centric).
View
Just as in MVC and MVP patterns, a view is the structure, layout, and appearance (UI) that the user sees on the screen.
ViewModel
A viewmodel is an abstraction of a view that exposes common properties and commands. MVVM has no MVC controller and no MVP presenter. Instead, it has a binder. In the view model, the binder communicates between the view and the data binder.
Advantages:
-
Low coupling: Views can be changed and modified independently of Model. A ViewModel can be bound to different views, and the Model can remain unchanged when the View changes, and the View can remain unchanged when the Model changes.
-
Reusability: You can put some View logic in a ViewModel so that many views can reuse that View logic.
-
Independent development: Developers can focus on business logic and data development, and designers can focus on page design.
-
Testable: Interfaces are harder to test, and tests can now be written against the ViewModel.
What is MVC?
MVC is one of the most widely used software architectures. MVC is generally divided into Model,View and Controller. This is mainly for the purpose of layering, separating responsibilities from each other. A View usually uses a Controller to communicate with a Model. The Controller is the coordinator of the Model and View, which are not directly related to the Model. It’s almost a one-way connection. M and V refer to the same meaning as M and V in MVVM. C, for Controller, is the page business logic. MVC is one-way communication. So the View and the Model, they have to be connected by the Controller.
A Model represents the application core (such as a database).
View displays (HTML pages).
Controller handles input (business logic).
The MVC pattern provides complete control over HTML, CSS, and JavaScript.
The Model is the part of the application that handles the application’s data logic. Typically, model objects are responsible for accessing data in a database.
A View is the part of the application that handles the display of data. Views are usually created from model data.
A 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.
Advantages:
- Low coupling
- High reusability
- Low lifecycle cost
- The deployment of fast
- High maintainability
- Advantageous software engineering management
Iii. Differences between MVC and MVVM:
The difference between MVC and MVVM is not that VM completely replaces C. Viewmodels exist to extract business logic from Controller, not to replace Controller. Other view operations and so on should still be implemented in Controller. In other words, MVVM enables reuse of business logic components.
- The Controller in MVC morphed into the ViewModel in MVVM
- MVVM shows the view layer through data rather than node operations
- MVVM mainly solves the problem that a large number of DOM operations in MVC reduce page rendering performance, slow loading speed and affect user experience
Source link: blog.csdn.net/weixin_4512…
Source: www.jianshu.com/p/ff6de219f…