Principles and differences between MVVM and MVC

1. What are MVVM and MVC?

MVVM(Model-View-ViewModel):

  • M: Move (data model)
  • V:View
  • VM: ViewModel is an object that synchronizes View and Model.
  • Take 数据模型数据双向绑定the idea as the core, so there is no connection between the View and the Model, and the interaction is through the ViewModel, and the interaction between the Model and the ViewModel is two-way, so the change of the view data will modify the data source at the same time, and the data source data Changes will also be immediately reflected on the View.

MVC(Model-View-Controller):

  • M: Model (data model), used to store data
  • V: View (view), which is the user interface
  • C: Controller is the coordinator of Model and View. Controller takes the data in Model and gives it to View.
  • MVC is a relatively intuitive architecture model, user operation -> View (responsible for receiving user input operations) -> Controller (business logic processing) -> Model (data persistence) -> View (feed back the results to View)

Two, MVC principle

Insert picture description hereThe Controller is responsible for displaying the data of the Model in View.
User operation> View (responsible for accepting user input operations)>Controller (business logic processing)>Model (data persistence)>View (feeding back the results to the user through the View)

Disadvantages:

  1. All business logic is operated in the Controller, the logic is complex and not conducive to maintenance,
  2. A large number of DOM operations reduce the page rendering performance, slow down the loading speed, and affect the user experience.
  3. When the Model frequently changes, it needs to be actively updated to the View; when the user's operation causes the Model to change, it is also necessary to synchronize the changed data to the Model. This work is not only cumbersome, but also difficult to maintain complex and changeable data states.

Due to the defects of the MVC model, the variant model of MVC derives the MVVM model


Three, MVVM principle

Insert picture description hereVM two-way binding: In the MVVM framework, View (view) and Model (data) cannot communicate directly, and there is an intermediary between them that acts as an observer.
When the user operates the View (view), the ViewModel perceives the change, and then notifies the Model to change accordingly; on the
contrary, when the Model (data) changes, the ViewModel can also perceive the change and make the View update accordingly. This back and forth process is known as two-way binding.

Guess you like

Origin blog.csdn.net/isfor_you/article/details/114374044