问题描述
超越 rad rad (drag-drag-drop and configure)构建用户界面的方式许多工具鼓励您可能会遇到三种设计模式,称为 model-view-controller ,和 model-view-view-viewmodel .我的问题有三个部分:
- 这些模式解决了什么问题?
- 它们如何相似?
- 他们有何不同?
推荐答案
model-view-presenter
在 MVP 中,主持人包含视图的UI业务逻辑.来自视图的所有调用直接委托给主持人.演示者也直接从视图中解耦,并通过接口对其进行对话.这是为了在单元测试中嘲笑视图. MVP的一个常见属性是必须有很多双向调度.例如,当某人单击"保存"按钮时,事件处理程序会委托主持人的" onsave"方法.保存完成后,主持人将通过其接口回电,以便显示保存已完成的视图.
MVP往往是在WebForms中实现分离演示的非常自然的模式.原因是该视图始终首先由ASP.NET运行时创建.您可以 model-dive -ViewModel .
有一个有关演示模型的MSDN文章在 wpf (前Prism)的复合申请指南=" http://msdn.microsoft.com/en-us/library/cc707862.aspx" rel =" noreferrer">分开的演示模式
其他推荐答案
这是对这些设计模式的许多变体的过度简化,但这就是我想考虑两者之间的差异的方式.
MVC
MVP
其他推荐答案
我不久前写了一个博客,请引用 todd Snyder关于两者之间的差异的出色帖子:
这是关键区别 图案:
MVP模式
- 视图更加松散地与模型耦合.主持人是 负责将模型绑定到 视图.
- 更容易单位测试,因为与视图的互动是通过 接口
- 通常会查看主持人一对一地图.复杂的观点可能有 多主持人.
MVC模式
- 控制器基于行为,可以在 视图
- 可以负责确定显示哪种观点
这是我能找到的网络上最好的解释.
问题描述
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:
- What issues do these patterns address?
- How are they similar?
- How are they different?
推荐答案
Model-View-Presenter
In MVP, the Presenter contains the UI business logic for the View. All invocations from the View delegate directly to the 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 tends to be a very natural pattern for achieving separated presentation in WebForms. The reason is that the View is always created first by the ASP.NET runtime. You can find out more about both variants.
Two primary variations
Passive View: The View is as dumb as possible and contains almost zero logic. A 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 that the Presenter uses to set the data. All state is managed in the Presenter and not the View.
- Pro: maximum testability surface; clean separation of the View and Model
- Con: more work (for example all the setter properties) as you are doing all the data binding yourself.
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: by leveraging data binding the amount of code is reduced.
- Con: there's a less testable surface (because of data binding), and there's less encapsulation in the View since it talks directly to the Model.
Model-View-Controller
In the MVC, the Controller is responsible for determining which View to display 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.
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.
Presentation Model
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 behavior for the view. This pattern is a very strong candidate for use in WPF applications and is also called Model-View-ViewModel.
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
其他推荐答案
This is an oversimplification of the many variants of these design patterns, but this is how I like to think about the differences between the two.
MVC
MVP
其他推荐答案
I blogged about this a while back, quoting on Todd Snyder's excellent post on the difference between the two:
Here are the key differences between the patterns:
MVP Pattern
- View is more loosely coupled to the model. The presenter is responsible for binding the model to the view.
- Easier to unit test because interaction with the view is through an interface
- Usually view to presenter map one to one. Complex views may have multi presenters.
MVC Pattern
- Controller are based on behaviors and can be shared across views
- Can be responsible for determining which view to display
It is the best explanation on the web I could find.