问题描述
我正在尝试使用 MVP 模式,但遇到了设计问题.我正在开发一个包含多个 UserControl 的应用程序.UserControls 本身彼此无关,仅代表实际模型的一个子集.根据我的阅读,人们倾向于说每个视图应该使用一个演示者.这似乎是有道理的,但如果我有 30 个用户控件,我真的想要 30 个演示者吗?另一方面,如果我有 1 个 Presenter 和 1 个 View 代表整个应用程序"视图,那么我将拥有臃肿的 View 和 Presenter 界面.然后每个视图都必须实现与它无关的方法.我的问题是,有没有更好的方法来处理多个用户控件,或者我应该为每个视图创建 1 个演示者?
I’m trying to use the MVP pattern and I’m running into a design problem. I’m developing an application that will have several UserControls. The UserControls themselves have nothing to do with one another and only represent a subset of the actual model. From what I’ve read, people tend to say you should use one Presenter per View. This seems to make sense, but if I have 30 UserControls, do I really want 30 Presenters? On the flip side, if I have 1 Presenter and 1 View that represent the entire "application" view, then I’ll have bloated View and Presenter interfaces. Then each View would have to implement methods that have nothing to do with it. My question is, is there a better way to handle multiple UserControls, or should I just create 1 Presenter for each View?
推荐答案
将相关的代码分组到一个对象中会更有意义.因此,在这种情况下,如果视图是相关代码的特定分组,那么演示者也会模仿这些分组.为不同的视图设置一个全局"演示者会将不相关的代码分组到一个对象中.它肯定也会使演示者的界面膨胀.查看单一责任原则.
It would make more sense to group the code that is related in one object. So, in this case, if the views are specific groupings of related code, then the presenter would also mimic these groupings. To have a "global" presenter for different views would group unrelated code in one object. It would definitely bloat the interface for the presenter as well. Check out the single responsibility principle.
现在,您可以拥有一个 Presenter Manager 类,或许可以让您访问每个演示者界面,就像接口隔离原则所述,通过继承(有一个实现许多演示者接口的全局具体演示者......这违反了单一职责)或聚合(每个接口都有单独的演示者并获取函数...因此,全局接口将是 get 函数)或两者的组合(全局演示者在某种程度上是一个适配器).
Now, you could have one Presenter Manager class perhaps that could give you access to each presenter interface, like the Interface Segregation Principle states, by either inheritance (have a global concrete presenter that implements many presenter interfaces..which kind of violates the single responsibilty) or aggregation (having individual presenters for each interface and get functions...thus the global interface would the the get functions) or a combination of both (global presenter being somewhat of an adapter).
我认为最好的解决方案是拥有 30 位不同的演示者.
I think the best solution though would just to have 30 different presenters.
这篇关于MVP 和多个用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!