Difference between MVC and MVP-Life is beautiful-Sohu blog

Source: Internet
Author: User

 

Paste it here for fear that it will not be found in the future. If it is useful for C #, you can look at it.

In his blog, Alex compared MVC with MVP:

[Translation ]:

Introduction to model view presenter vs Model View Controller

In my work, I often need to deal with some problems that developers cannot clearly understand.MVCAndMVPProblems arising from the use of different modes. In this article, I will explain my understanding of the differences between the two.
In the n-layer architectureMVCThe/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 understanding of the three parts of the 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 )". The appearance here refers to"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.

MVCThe core of the/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 MVC(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, here, the formMVCIs 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"ModeIn the general senseMVCMode

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 notifications is described below as "passive-MVC.]

"Passive-MVC"Mode
And activeMVCThe difference 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.

MVPMode

And "passive-MVCMode "is very similar, the difference is"View does not use model". InMVPIn the mode, the view and model are completely separated and they interact through the presenter.
The presenter and controller are very similar, but they also have some differences:
1. Presenter processes user operations sent from the view (inMVCThe image itself handles these operations)
2. It uses updated data to update the model (passiveMVCThe Controller only notifies the view to retrieve new data from the updated model.MVCIn the model Notification View to update the display, the controller does not need to do the work)
3. Check for model updates (and passiveMVCSame)
4.MVC(The main difference) get data from the model and then send them to the view.
5.MVCTo inform the view of the updates.
6.MVC) Rendering the view with presenter.

MVPBenefits

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)

MVPProblems

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.

Difference between MVC and MVP-Life is beautiful-Sohu blog

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.