MVC and MVP vs. MVVM

Source: Internet
Author: User

==MVC,MVP and MVVM are common software architecture design patterns that improve the way code is organized by separating the points of interest = =

Similarities and differences in MVC, MVP, and MVVM

The different parts are C (Controller), P (Presenter), VM (View-model), and the same part is MV (Model-view)

    • The model layer is used to encapsulate data related to the business logic of the application and how the data is processed
    • View as a layer of views, primarily responsible for the presentation of data
Mvc


Solid lines represent method calls, and dashed lines represent event notifications.

    1. View Transfer command to Controller
    2. Controller needs Model to change state after completion of business logic
    3. Model sends new data to View, user gets feedback

After the user's view operation, the view captures this operation, which will transfer the right of processing to the controller (Pass calls), the controller will preprocess from the view data, decide which model to call the interface And then the model executes the relevant business logic, and when the model is changed, it notifies view;view through the observer pattern to receive the model change message through the viewer mode, and then requests the latest data from the model. And then re-update the interface

Two design patterns are involved:

    • The observer pattern between view and model, view model, is registered in advance on this model so that view can understand the changes that occur on the data model.
    • The policy mode between view and controller

The essence of the MVC pattern is the 3rd: Model updates are communicated to the view through the observer pattern, which can be either pub/==sub or triggered events==

The business logic of the MVC pattern focuses on the controller, and the front-end view has the ability to handle user events independently, which becomes bloated when each event flows through the controller. and MVC in the view and controller is generally one by one corresponding, tied up to represent a component, the view and the controller of the too close connection to make the controller reusability is a problem.

MVC Benefits:

Separation of business logic and presentation logic, high degree of modularity. And when the application logic needs to change, do not need to change the business logic and presentation logic, only need to switch controller to another controller on the line (swappable controller).
The Observer pattern can be updated at the same time as multiple views.

MVC drawbacks:
Controller testing is difficult. Because view synchronization operations are performed by the view itself, view can only be run in a UI-only environment. In the absence of a UI environment for unit testing of controllers, the application logic correctness is not verifiable: when the model is updated, the update operation of the view cannot be asserted.
View cannot be modular. View is strongly dependent on a particular model, and it is difficult to extract the view as a reusable component of another application. Because the domain model of different programs is not the same

MVC Model 2 Back-end MVC is often called MVC thinkphp, etc.

The service end receives the request from the client, the service side passes this request to the specific controller through the routing rule, the controller executes the corresponding application logic, the model carries on the operation, the model executes the business logic, then uses the data to render the specific template, Returned to the client.

Because the HTTP protocol is a simplex protocol and is stateless, the server cannot push data directly to the client. The server-side model changes cannot be communicated to the client unless the client initiates the request again. So you can see that the classic Smalltalk-80 MVC model tells the view update through the Observer pattern that this ring is ruthlessly broken and cannot be called a strict MVC.

MVP presenter Beauty [PR?? ZENT?R] presenter, studio member; recommended by; The proposed person; A giver;

MVP (Model-view-presenter) is an improvement of the MVC model, proposed by IBM's subsidiary, Taligent. And MVC are the same: Controller/presenter is responsible for business logic, Model Management data, view is responsible for the display.

Although in MVC, view is directly accessible to model, the view in the MVP does not use the model directly, but instead provides an interface for presenter, allowing presenter to update the model, and then update the view through the Observer mode.

In contrast to MVC, the MVP model separates views and models by decoupling the view and model, which makes the delineation of responsibilities clearer; Since view does not rely on model, it can be pulled out of the view to make components, it only needs to provide a series of interfaces to the upper operation.

Presenter as the "middleman" between view and model, in addition to the basic business logic, there is a lot of code that needs to "manually synchronize" the data from view to model and from model to view, so that the presenter appears heavy and difficult to maintain. And because there is no data binding, if the demand for presenter view rendering increases, it has to pay too much attention to a particular view, and presenter needs to change once the demand for the views changes.

MVVM

MVVM (Model-view-viewmodel) was first proposed by Microsoft, ViewModel refers to the model of view--the models of views.

MVVM can be seen as a special MVP (Passive View) mode, or an improvement to the MVP model.

MVVM automates the synchronization logic of view and model. Previously presenter-Responsible view and model synchronization is no longer manual, but to the framework to provide the data binding function to be responsible, just tell it the view display data corresponding to which part of the model.

In Vue, data is Model,view, which is the template language, and the updated view is the VM.

Data binding
    • Data Hijacking (Vue)
    • Publish-Subscribe mode (Knockout, Backbone)
    • Dirty value Check (Angular)

MVC and MVP vs. MVVM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.