简体   繁体   English

在MVP设计模式中,一个视图可以有多个演示者吗?

[英]In MVP design pattern, can a view have more than one presenter?

I have studied examples of MVP. 我研究了MVP的例子。 None of the examples I found has a complex Form. 我找到的所有例子都没有复杂的表格。 In real life a form can get very complicated (specially if the client you are working for does not understand the "single responsibility principle"). 在现实生活中,表格可能变得非常复杂(特别是如果您工作的客户不理解“单一责任原则”)。

Keeping this in mind, does the following example conforms to MVP principle? 记住这一点,下面的例子是否符合MVP原则?

Form Code - The form implements two interfaces, one for each presenter. 表单代码 - 表单实现两个接口,每个接口一个。

    public partial class ExampleForm : Form, ILookupView, IExampleView
    {
        //Constructor
        public ExampleForm()
        {
           InitializeComponent();

            var presenterEquipment = new EquipmentPresenter(this);
            presenterEquipment.PopulateEquipmentLookup();

            var presenterMain = new ManageComponentsPresenter(this);
            presenterMain.SetInitialValues();
        }
    }

If not what is the best way to handle large and complex forms? 如果不是处理大型复杂表格的最佳方式是什么?

Many Thanks. 非常感谢。

I would split the view into smaller views, implemented as user controls and compose the large view of the small ones. 我会将视图拆分为较小的视图,实现为用户控件并组成小视图的大视图。

However, technically, I don't see any reasons why your approach wouldn't work. 但是,从技术上讲,我没有看到任何原因导致您的方法不起作用。 You can have your large view implementing several interfaces and assign multiple presenters to different "subviews". 您可以让大视图实现多个接口,并将多个演示者分配给不同的“子视图”。

It has been my philosophy to make the design patterns match the needs of your app. 我的理念是让设计模式与您的应用程序的需求相匹配。 Also My understanding of MVP, MVC is constantly growing. 我对MVP,MVC的理解也在不断增长。 To answer your question though The relationship between the models of MVP are. 回答你的问题虽然MVP模型之间的关系是。

One View has One Presenter which talks to zero or more Models. One View有一个Presenter可以与零个或多个模型进行对话。 The likely situation if you have a need for multiple Presenters for one view then those views are probably good candidates to be separated out into smaller subviews that are imported by a larger view. 如果您需要为一个视图使用多个Presenter,则可能出现这种情况,这些视图很可能被分离为较大视图导入的较小子视图。 Or that there is business logic that can be separated out to one or more models 或者存在可以分离出一个或多个模型的业务逻辑

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

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