简体   繁体   English

模型 - 视图 - 展示器模式中“视图”的目的是什么?

[英]What is the purpose of the “View” in the Model-View-Presenter pattern?

I understand the general reason why the Model-View-Presenter pattern is a good pattern. 我理解模型 - 视图 - 展示器模式是一个好模式的一般原因。 It separates the concern so that the same parts of code that deal with presenting information to the user do not worry about calculations, etc. My quesion is why do we need a view that essentially does nothing? 它将关注点分开,以便处理向用户呈现信息的相同代码部分不用担心计算等问题。我的问题是为什么我们需要一个基本上什么都不做的视图? All the view does is tell the presenter to do work and get the result back from the presenter. 所有观点都告诉演示者做好工作并从演示者那里获得结果。 For example, in my C# views, I often just have a bunch of event handlers that call delegates that are implemented in the presenter. 例如,在我的C#视图中,我经常只有一堆事件处理程序来调用在演示者中实现的委托。 Why not just combine the view and the presenter? 为什么不组合视图和演示者? No concern is really separated because the view does not have any concern. 没有关注真的是分开,因为视图没有任何问题。

Two important advantages 两个重要的优点

  • We can write multiple views or replace views easily (for example WinForms => WPF implementation) 我们可以轻松编写多个视图或替换视图(例如WinForms => WPF实现)

  • We increase the testability by creating test views for unit tests 我们通过为单元测试创​​建测试视图来提高可测试性

Your View should simply be a way to get data to and from the user. 您的View应该只是一种从用户那里获取数据的方法。 Anything else that is not specifically related to that functionality should be pushed down into the Presenter (or model if it needs be). 任何与该功能无关的其他内容都应该下推到Presenter(或模型中,如果需要)。 The presenter handles what the view gets but it should never be concerned with what view does once it gets it. 演示者处理视图所获得的内容,但它永远不应该关注视图获取后的视图。

The presenter is an attempt to look at your "UI" in a more semantic manner. 演示者试图以更加语义的方式查看您的“UI”。 You may have 2 textboxes on your View but your Presenter sees Name and Surname. 您的视图中可能有2个文本框,但您的Presenter会看到名称和姓氏。 The idea should be that with the minimal amount of fuse you could lift the View off an pop another one on. 我们的想法应该是,使用最少量的保险丝,您可以将视图从另一个视频中移开。

I don't know how much I agree with that and tend to prefer MVVM where you don't have Presenters, instead you have ViewModels which I feel are a better way to define the above abstraction. 我不知道我多少同意这一点,并倾向于更喜欢MVVM,而你没有Presenters,而是你有ViewModel,我认为这是定义上述抽象的更好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM