GUI设计模式,MVP,选项卡控件

发布于 2024-09-12 19:23:30 字数 777 浏览 5 评论 0原文

我有一个应用程序,其窗口类似于下面的窗口。

替代文本 http://a.imageshack.us/img137/7481/screenshotxh.jpg< /a>

这里的要求是,当用户单击保存按钮时,所有内容都必须保存。 “保存”和“重置”按钮对于所有选项卡都是“通用”的。因此,当选择“个人信息”选项卡并单击“保存”时,程序还应该保存在“朋友”选项卡中所做的更改以及在“工作历史记录”选项卡中所做的更改。

该应用程序已具有以下代码,我想保留此代码:

-PersonalInformationView 、 PersonalInformationPresenter、PersonalInformationModel

-FriendsView、FriendsPresenter、FriendsModel

-EmploymentHistoryView、EmploymentHistoryPresenter、EmploymentHistoryModel

每个演示者都有一个 Save 方法。

问题是考虑到我想保留我已有的代码,什么是一个好的设计模式。另外,我希望这个窗口也有模型、视图、演示者。或者也许我应该稍微改一下我的问题:在编程 MVP 时包含“子视图”、“子演示者”的最佳方式是什么

问候, 麦德塞布

I have an application that has a window similar to the one bellow .

alt text http://a.imageshack.us/img137/7481/screenshotxh.jpg

The requirement here is that when the user clicks the Save button everything has to get saved. The "Save" and "Reset" buttons are "common" to all tabs. Hence, when the "Personal Information" tab is selected and "Save" is clicked the program should also save changes made in the "Friends" tab and changes made in the "Employment History" tab.

The app already has code for the following and I want to keep this code:

-PersonalInformationView , PersonalInformationPresenter, PersonalInformationModel

-FriendsView, FriendsPresenter, FriendsModel

-EmploymentHistoryView, EmploymentHistoryPresenter, EmploymentHistoryModel

Each presenter has a Save method.

The question is what would be a good design pattern to use taking into consideration that I want to keep the code I already have. Also, I want this window to have model, view, presenter as well. Or maybe I should rephrase my question a bit: what's the best way of including "sub-views", "sub-presenters" when programming MVP ?

Regards,
MadSeb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-09-19 19:23:30

我个人建议创建一个抽象接口、ISaveable 或 osmething,并确保每个演示者都实现此接口,而不是将每个演示者作为 ISaveable 对象进行遍历并保存每个演示者。

I personally would suggest making an abstract interface, ISaveable, or osmething and ensure that each of the presenters implement this, than go through each presenter as an object of ISaveable and save each one.

拿命拼未来 2024-09-19 19:23:30

我会让你的新演示者将你的子演示者作为构造函数参数,例如:

class DialogPresenter {

    private readonly IDialogView view;
    private readonly PersonalInformationPresenter personal;
    private readonly FriendsPresenter friends;
    private readonly EmploymentHistoryPresenter history;

    void DialogPresenter(IDialogView view, PersonalInformationPresenter personal, FriendsPresenter friends, EmploymentHistoryPresenter history) {
        this.view = view;
        this.personal = personal;
        this.friends = friends;
        this.history = history;
    }

    bool Display() {
        this.personal.Display();
        this.friends.Display();
        this.history.Display();

        return this.view.Display() == DialogResult.Ok;
    }

    void Save() {
        this.personal.Save();
        this.friends.Save();
        this.history.Save();
    }
}

当然,如果你的演示者之间有一个公共接口,则可以像这样简化(并使其更具可扩展性):

class DialogPresenter {

    private readonly IDialogView view;
    private readonly IPresenters[] presenters;

    void DialogPresenter(IDialogView view, IPresenters[] presenters)
    {
        this.view = view;
        this.presenters = presenters;
    }

    bool Display() {
        foreach (var item in this.presenters)
            item.Display();

        return this.view.Display() == DialogResult.Ok;
    }

    void Save() {
        var validation = new List<string>();

        foreach (var item in this.presenters)
            validation.AddRange(item.Validate());

        if (validation.Count > 0) {
                _view.ShowErrors(validation);
                return;
        }

        foreach (var item in this.presenters)
            validation.AddRange(item.Save());
    }
}

编辑:< /强>
调用代码将是这样的:

void DisplayForm() {

    using (var frm = new frmDisplay) {

        //or just use DI to get the models etc
        var personal = new PersonalInformationPresenter(personalModel, frm.PersonalTab);    //some properties to expose your views
        var friends = new FriendsPresenter(friendslModel, frm.FriendsTab);
        var history = new EmploymentHistoryPresenter(employmentHistoryModel, frm.HistoryTab);

        var presenter = new DialogPresenter(frm, personal, friends, history);
        if (presenter.Display()) {    
            presenter.Save();
        }
    }
}

希望这有一些启发/帮助:)

I would make your new presenter take in your sub presenters as constructor arguments, something like:

class DialogPresenter {

    private readonly IDialogView view;
    private readonly PersonalInformationPresenter personal;
    private readonly FriendsPresenter friends;
    private readonly EmploymentHistoryPresenter history;

    void DialogPresenter(IDialogView view, PersonalInformationPresenter personal, FriendsPresenter friends, EmploymentHistoryPresenter history) {
        this.view = view;
        this.personal = personal;
        this.friends = friends;
        this.history = history;
    }

    bool Display() {
        this.personal.Display();
        this.friends.Display();
        this.history.Display();

        return this.view.Display() == DialogResult.Ok;
    }

    void Save() {
        this.personal.Save();
        this.friends.Save();
        this.history.Save();
    }
}

Of course, if your presenters had a common interface between them, this could be simplified (and made more extendable) like so:

class DialogPresenter {

    private readonly IDialogView view;
    private readonly IPresenters[] presenters;

    void DialogPresenter(IDialogView view, IPresenters[] presenters)
    {
        this.view = view;
        this.presenters = presenters;
    }

    bool Display() {
        foreach (var item in this.presenters)
            item.Display();

        return this.view.Display() == DialogResult.Ok;
    }

    void Save() {
        var validation = new List<string>();

        foreach (var item in this.presenters)
            validation.AddRange(item.Validate());

        if (validation.Count > 0) {
                _view.ShowErrors(validation);
                return;
        }

        foreach (var item in this.presenters)
            validation.AddRange(item.Save());
    }
}

Edit:
Calling code would be something like this:

void DisplayForm() {

    using (var frm = new frmDisplay) {

        //or just use DI to get the models etc
        var personal = new PersonalInformationPresenter(personalModel, frm.PersonalTab);    //some properties to expose your views
        var friends = new FriendsPresenter(friendslModel, frm.FriendsTab);
        var history = new EmploymentHistoryPresenter(employmentHistoryModel, frm.HistoryTab);

        var presenter = new DialogPresenter(frm, personal, friends, history);
        if (presenter.Display()) {    
            presenter.Save();
        }
    }
}

Hope that is of some inpsiration/help :)

走过海棠暮 2024-09-19 19:23:30

我的建议是使用 save 方法创建 ISaveableView。
您的每个视图都将实现该接口。
我猜测您的选项卡实现了您所描述的视图。当您单击保存按钮时,您可以将活动选项卡投射到 ISaveableView 并调用其保存方法

My suggestion is to create ISaveableView with save method.
Each of your views will implement the interface.
I am guessing that your tabs implements the views you've described. When you click the save button you can cast the active tab to ISaveableView and call its save method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文