GUI设计模式,MVP,Tab控件

时间:2010-08-04 13:30:46

标签: design-patterns model view tabs mvp

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

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

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

该应用已经包含以下代码,我想保留此代码:

-PersonalInformationView,PersonalInformationPresenter,PersonalInformationModel

-FriendsView,FriendsPresenter,FriendsModel

-EmploymentHistoryView,EmploymentHistoryPresenter,EmploymentHistoryModel

每个演示者都有一个保存方法。

问题是考虑到我想要保留我已有的代码,使用什么样的好设计模式。此外,我希望这个窗口也有模型,视图,演示者。或者我可能会稍微改一下我的问题:在编写MVP 时包含“子视图”,“子演示者”的最佳方法是什么?

此致 MadSeb

3 个答案:

答案 0 :(得分:1)

我个人建议创建一个抽象接口,ISaveable或osmething,并确保每个演示者实现这一点,而不是通过每个演示者作为ISaveable的对象并保存每个。

答案 1 :(得分:0)

我的建议是使用save方法创建ISaveableView。 您的每个视图都将实现该界面。 我猜你的标签实现了你所描述的观点。单击“保存”按钮时,可以将活动选项卡强制转换为ISaveableView并调用其保存方法

答案 2 :(得分:0)

我会让你的新主持人接受你的子演示者作为构造函数参数,例如:

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();
        }
    }
}

希望有一些帮助/帮助:)