被动视图和显示逻辑 [英] passive view and display logic

查看:90
本文介绍了被动视图和显示逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC和MVP和类似的模式中,通常采用被动视图"的方法,这种方法尽可能愚蠢(包含尽可能少的逻辑).这应该有利于单元测试,并创建更清晰的视图和模型分离.

In MVC and MVP and similar patterns there's often the approach of the "passive view" which is as stupid (contains as few logic) as possible. This should facilitate unit testing and create a clearer separation of view and model.

我知道这些模式具有非常不同的风格,尤其是对MVP的理解似乎因文章而异.因此,我的问题不是我如何正确实现此模式".

I know that those patterns come in very different flavours and especially the understanding of MVP seems to differ from article to article. Therefore my question is not "how do i implement this pattern correctly".

我想改善视图和模型的分离,并为应用程序提供更好的可测试性.因此,我想采取被动的看法.但是我的问题是,您将把仅与视图相关的逻辑放在哪里?就像textviewer应该在滚动条移动时滚动文本一样.您会将此逻辑放到Presenter中吗? 假设textviewer具有一些扩展功能.例如在textpass上设置标记.显然,将其逻辑放入Presenter中是有意义的.但是,如果将它与视图的所有直接"逻辑(例如滚动文本)混合使用,Presenter可能会变得很大,这也不是一个很好的设计.

I want to improve view and model separation and go for better testability of the application. Therefore i'd like to go for a passive view. But my question is, where would you put logic that is clearly only view related? like a textviewer should scroll the text when the scrollbar is moved. would you put the logic for this into the Presenter? Let's say the textviewer has some extended functionality. like setting markings on textpassages. The logic for this makes clearly sense to be put into the Presenter. However, if it is mixed with all the 'direct' logic of the view (like scrolling the text) the Presenter could become very big, which is also not really a nice design.

所以我的问题是在哪里放置与被动视图相关的显示逻辑以及在Presenter中要混合的功能.

So my question is where to put display related logic of a passive view and what functionallity to mix in the Presenter.

谢谢!

推荐答案

如果在用户滚动时需要从模型中获取更多数据,我会在演示者中添加滚动条逻辑.即使没有,该视图也应通知演示者.

I'd put scrollbar logic in the presenter if it needs to fetch more data from the model when the user scrolls. Even if not, the view should notify the presenter.

有时,GUI API会自行处理滚动.如果是这种情况,那么您所能做的就是通知演示者中的存根(不执行任何操作).该视图不知道对演示者重要的是什么.

Sometimes the GUI API handles scrolling by itself. If that's the case then all you could do is notify a stub within the presenter (which does nothing). The view has no idea of what's important to the presenter.

演示者不必是单个整体类.最好为不同的职责聚合不同的对象.从测试的角度来看,这实际上是一件好事,您可以在其中模拟掉所有当前未测试的对象.

The presenter does not have to be a single monolithic class. Preferably, you'd aggregate different objects for different responsibilities. This is actually good from a testing point of view, where you can mock out all the objects that you are not currently testing.

这篇关于被动视图和显示逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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