MVP mode and MVC mode)

Source: Internet
Author: User

MVP
It evolved from the classic MVC model. Their basic ideas are the same: Controller/presenter is responsible for logical processing, model provides data, and view is negative.
Responsibility display. As a new model, MVPs and MVC have a major difference: in MVPs, views do not directly use models, and the communication between them is through presenter.
(Controller in MVC), all interactions occur within the presenter, and in MVC, the view will read data from the direct model rather than through
Controller. Alex in his blog for the comparison between the two very intuitive and clear, the original can be found below: http://ameleta.spaces.live.com/blog/cns! 5f6425345a821420! 163. Entry

[Translation ]:
Model View presenter vs Model View Controller

Introduction

In my work, I often need to deal with problems that arise when developers fail to clearly understand the differences between MVC and MVP patterns. In this article, I will elaborate on
Understanding of the differences between the two.
In the n-layer architecture, the MVC/P mode is only used for presentation layer (presentation ).
Layer), understanding this is very important. These two modes are not about how to build a data layer and a service layer.
Data is separated from the user interface (View) and how the user interface interacts with the data. Use these modes to remove your program
The presentation layer depends on data and control logic, so that you can freely change the presentation layer.

General concepts of these two models

1. model indicates the data model and business logic
Logic ). A model is not always a dataset, A able, or something like this. It represents a class of components or classes. These components or classes
Data can be provided to the outside, and data can be obtained from the outside and stored somewhere. With a simple understanding, you can think of a model as a "Facade
Class )". Note: The appearance here refers to the appearance in "appearance mode. The general function of appearance is to provide high-level easy-to-use access interfaces for a complex subsystem. See the figure below.
To understand its principles:

 

2. View: present the data layer to the user. The general view only contains the user interface (UI), not the interface logic. Ratio
For example, a page containing controls in Asp.net is a view. A view can read data from a model, but cannot modify or update a model.
3. The presenter/controller contains the logic for updating the model based on the user's behavior in the view. The view only controls user behavior notification.
The Controller obtains data from the view and sends the data to the model.

The core of the MVC/P mode is to separate the model from the View/controller so that the model is independent of it.
Therefore, the model does not include reference to views and controls.
What is the model view presenter mode?

1. To enable the View Interface to interact with the model and controller, the Controller executes some initialization events.
2. the user performs some operations through the view (User Interface ).
3. The controller processes user behavior (which can be implemented in observation mode) and notifies the model to update.
4. The model triggers some events to send the Change Notification View.
5. view the changes to the Model and display the new model data.
6. user interfaces are waiting for further user operations

There are several key points for this model:
1. views do not use controllers to update models. The controller processes user operations sent from the view and updates data through interaction with the model.
2. The controller can be integrated with the view. In visual studion
This is the default form of forms. For example, double-click a button, write the processing logic in its event, and write the processed data back to the model. When processing logic here
It should be the function of the controller, but we did not specifically write a controller to do this. Instead, we accepted the default Processing Method of Vs and wrote it in the form code, here, the form is in MVC
Is a view. Therefore, the default vs processing method is to combine controllers and views .]
3. The controller does not include the rendering logic of the view (rendering logic)

"Active-MVC" Mode
And also the MVC pattern in the general sense.

 

 

 

Why is it active? View is monitored by itself instead of waiting for the Controller to notify it to update the model and then retrieve data from the model and update the display.
Update the model (if the observer mode is used) or actively ask whether the model is updated. The previous method of waiting for controller notification is the implementation of "passive-MVC" described below.
Method .]

"Passive-MVC" Mode

The difference with active MVC is:
1. The model knows nothing about views and controllers. It is only used by them.
2. The controller uses the view and notifies it to update the data display.
3. The view does this only when the Controller notifies it to fetch data from the model (the view does not subscribe to or monitor model updates)
4. The Controller is responsible for processing model data changes
5. The controller can contain rendering logic for the view.

 

 

MVP Mode

Similar to the passive-MVC mode, the difference is that the view does not use a model ". In MVP mode, the Visual View and model are completely separated.
Presenter.
The presenter and controller are very similar, but they also have some differences:
1. Presenter processes the user operations sent from the view (in MVC, the view itself handles these operations)
2. It updates the model with updated data (in passive MVC, the Controller only notifies the view to retrieve new data from the updated model, the model Notification View in the active MVC updates the display, and the Controller does not need
Work done)
3. Check for model updates (same as passive MVC)
4. (The main difference with MVC) fetch data from the model and send them to the view.
5. (The main difference with MVC) inform the view of the update.
6. use presenter to render the view (different from MVC)

 

 

 

Advantages of MVP

1. The model and view are completely separated. We can modify the view without affecting the model.
2. The model can be used more efficiently, so interaction occurs in one place-inside the presenter.
3. We can use a presener for multiple views without changing the presenter logic. This feature is very useful because views change more frequently than models.
4. If we put the logic in the presenter, we can test the logic (unit test) from the user interface ).

MVP Problems

Because the rendering of the view is placed in the presenter, the interaction between the view and persenter is too frequent.

Another point you need to understand is that if the presenter renders the view too much, it will often make it too closely associated with a specific view. Once the view needs to be changed
The presenter also needs to be changed. For example, if the presenter originally used to present HTML needs to present PDF files, the view may need to be changed.

Appendix:

Http://www.microsoft.com/china/msdn/library/architecture/architecture/architecturetopic/MVP.mspx? MFR = true

Http://www.codeproject.com/useritems/ModelViewPresenter.asp

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.