什么是MVP和MVC有什么区别? [英] What are MVP and MVC and what is the difference?

查看:126
本文介绍了什么是MVP和MVC有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当查看 RAD (拖放和配置)的方式来构建用户界面许多工具鼓励您可能会遇到三种设计模式:模型视图控制器模型视图-Presenter Model-View-ViewModel 。我的问题有三个部分:

When looking beyond the RAD (drag-drop and configure) way of building user interfaces that many tools encourage you are likely to come across three design patterns called Model-View-Controller, Model-View-Presenter and Model-View-ViewModel. My question has three parts to it:


  1. 这些模式发生什么问题?

  2. 他们相似?

  3. 他们有什么不同?


推荐答案

h2> Model-View-Presenter

MVP 中,Presenter包含View的UI业务逻辑。 View代表的所有调用直接发送给Presenter。演示者也直接从视图中解耦,并通过界面与之对话。这是为了允许在单元测试中嘲笑View。 MVP的一个常见属性是必须进行大量的双向调度。例如,当有人点击保存按钮时,事件处理程序将委托给Presenter的OnSave方法。保存完成后,Presenter将通过其界面回调View,以便View可以显示保存完成。

Model-View-Presenter

In MVP, the Presenter contains the UI business logic for the View. All invocations from the View delegate directly to Presenter. The Presenter is also decoupled directly from the View and talks to it through an interface. This is to allow mocking of the View in a unit test. One common attribute of MVP is that there has to be a lot of two-way dispatching. For example, when someone clicks the "Save" button, the event handler delegates to the Presenter's "OnSave" method. Once the save is completed, the Presenter will then call back the View through its interface so that the View can display that the save has completed.

MVP往往是在Web窗体中实现分离显示的一种非常自然的模式。原因是View始终由ASP.NET运行时创建。您可以了解有关这两种变体的更多信息

MVP tends to be a very natural pattern for achieving separated presentation in Web Forms. The reason is that the View is always created first by the ASP.NET runtime. You can find out more about both variants.

被动视图:视图尽可能愚蠢并且包含几乎零逻辑。演讲者是一个中间人,谈论视角和模型。视图和模型被完全屏蔽。该模型可能引发事件,但Presenter订阅它们以更新View。在被动视图中,没有直接的数据绑定,而是View公开了Presenter用来设置数据的setter属性。所有状态都在演示者管理,而不是视图。

Passive View: The View is as dumb as possible and contains almost zero logic. The Presenter is a middle man that talks to the View and the Model. The View and Model are completely shielded from one another. The Model may raise events, but the Presenter subscribes to them for updating the View. In Passive View there is no direct data binding, instead the View exposes setter properties which the Presenter uses to set the data. All state is managed in the Presenter and not the View.


  • Pro:最大可测性曲面;视图和模型的清晰分离

  • Con:更多的工作(例如所有的setter属性),因为你正在做所有的数据绑定自己。

监督控制器:演示者处理用户手势。视图通过数据绑定直接绑定到模型。在这种情况下,Presenter的工作是将模型传递给View,以便它可以绑定到它。演示者还将包含按钮,导航等手势的逻辑。

Supervising Controller: The Presenter handles user gestures. The View binds to the Model directly through data binding. In this case it's the Presenter's job to pass off the Model to the View so that it can bind to it. The Presenter will also contain logic for gestures like pressing a button, navigation, etc.


  • Pro:通过利用数据绑定减少代码量。

  • Con:可见表面(因为数据绑定)较少,因为直接与模型直接对应,所以View中的封装少。

MVC 中,控制器负责确定哪些视图显示为响应任何操作,包括应用程序何时加载。这不同于MVP,其中动作通过视图路由到演示者。在MVC中,View中的每个动作都与调用Controller一起执行。在网络中,每个操作都涉及对另一边的一个URL的调用,其中Controller有一个响应。一旦控制器完成处理,它将返回正确的视图。在应用程序的整个生命周期中,序列继续以这种方式继续:

In the MVC, the Controller is responsible for determining which View is displayed in response to any action including when the application loads. This differs from MVP where actions route through the View to the Presenter. In MVC, every action in the View correlates with a call to a Controller along with an action. In the web each action involves a call to a URL on the other side of which there is a Controller who responds. Once that Controller has completed its processing, it will return the correct View. The sequence continues in that manner throughout the life of the application:


    Action in the View
        -> Call to Controller
        -> Controller Logic
        -> Controller returns the View.

MVC还有一个很大的区别就是View不直接绑定到Model。该视图简单呈现,完全无国籍。在MVC的实现中,View通常在代码背后不会有任何逻辑。这与MVP是相反的,因为如果View没有委托给Presenter,那么它绝对是必要的,所以它永远不会被调用。

One other big difference about MVC is that the View does not directly bind to the Model. The view simply renders, and is completely stateless. In implementations of MVC the View usually will not have any logic in the code behind. This is contrary to MVP where it is absolutely necessary because, if the View does not delegate to the Presenter, it will never get called.

另一个要查看的模式是演示模型模式。在这种模式下,没有主持人。相反,View直接绑定到演示模型。演示模型是专为视图制作的模型。这意味着这个模型可以揭示一个永远不会放在域模型上的属性,因为它会违反分离关系。在这种情况下,演示模型绑定到域模型,并且可以订阅来自该模型的事件。视图然后订阅来自演示模型的事件,并相应地进行更新。演示模型可以显示视图用于调用操作的命令。这种方法的优点是,您可以基本上删除代码隐藏,因为PM完全封装了视图的所有行为。此模式是WPF应用程序中非常强大的候选者,也称为模型视图-ViewModel

One other pattern to look at is the Presentation Model pattern. In this pattern there is no Presenter. Instead the View binds directly to a Presentation Model. The Presentation Model is a Model crafted specifically for the View. This means this Model can expose properties that one would never put on a domain model as it would be a violation of separation-of-concerns. In this case, the Presentation Model binds to the domain model, and may subscribe to events coming from that Model. The View then subscribes to events coming from the Presentation Model and updates itself accordingly. The Presentation Model can expose commands which the view uses for invoking actions. The advantage of this approach is that you can essentially remove the code-behind altogether as the PM completely encapsulates all of the behaviour for the view. This pattern is a very strong candidate for use in WPF applications and is also called Model-View-ViewModel.

有一个关于演示模型的MSDN文章分离的演示文稿格式的WPF的复合应用指南(前Prism)

There is a MSDN article about the Presentation Model and a section in the Composite Application Guidance for WPF (former Prism) about Separated Presentation Patterns

这篇关于什么是MVP和MVC有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆