MVP mode and MVC Mode

Source: Internet
Author: User

MVPs 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 responsible for 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. In MVC, the view reads data from the direct model rather than through the 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 explain my understanding of the differences between the two.
In the n-layer architecture, the MVC/P mode is only used for the presentation layer. It is important to understand this. These two modes are not about how to build the data layer and service layer, but about how to extract data from the user interface (view) and how the user interface interacts with the data. The use of these modes allows you to remove the dependency of the presentation layer on data and control logic in your program, so that you can freely change the presentation layer.

General concepts of these two models

1. model indicates the data model and business logic ). A model is not always a dataset, A able, or something like this. It represents a class of components or classes that can provide external data, data can also be retrieved 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. You can refer to the figure below to understand its principle:

 

2. View: present the data layer to the user. The general view only contains the user interface (UI), not the interface logic. 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 informs the controller of user behavior, and 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 them. Therefore, the model does not include reference to the view and control.
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. The default Processing Method for Windows Forms in visual studion is as follows. For example, double-click a button, write the processing logic in its event, and write the processed data back to the model. The logical processing time here should be the controller function, but we did not specifically write a controller to do this. Instead, we accepted the default vs processing method and wrote it in the form code, the form here is a view in MVC. 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" ModeAnd also the MVC pattern in the general sense.

 

Why is it active? The view does not wait for the Controller to notify the model to update the data from the model and update the display. Instead, it monitors the update of the model (if in observer mode) or actively asks whether the model is updated. The previous method of waiting for controller notification is the "passive-MVC" implementation method described below .]

"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 view and model are completely separated. they interact with each other through 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 controller does not need to update the display in the model Notification View of the active MVC)
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.