想象一个简单的用户控件,它有一个文本框(客户名称)和两个按钮(保存/取消)。
此 UserControl 位于带有另外两个按钮的父控件中(PreviousCustomer/NextCustomer)
问题是我不知道在哪里将模型传递给演示者。我是否通过视图构造函数?如果模型发生变化(点击了 PreviousCustomer/NextCustomer)怎么办?我每次都创建一个新视图吗?如果 UserControl 与许多孩子很复杂,这似乎很浪费。我可以在视图和演示者中实现 UpdateModel 方法,还是不正确?
这是我的代码:
public interface IModel
{
string CustomerName { get; set; }
}
public class Presenter
{
private IView _view;
private IModel _model;
public Presenter(IView view, IModel model)
{
_view = view;
_model = model;
_view.CustomerName = _model.CustomerName;
}
public void Save()
{
_model.CustomerName = _view.CustomerName;
}
public void Cancel()
{
_view.CustomerName = _model.CustomerName;
}
public void UpdateModel(IModel newModel)
{
_model = newModel;
_view.CustomerName = _model.CustomerName;
}
}
public interface IView
{
string CustomerName { get; set; }
}
public class View : UserControl, IView
{
private Presenter _presenter;
public string CustomerName
{
get { return CustomerNameEditBox.Text; }
set { CustomerNameEditBox.Text = value; }
}
//Is this the right place to pass in the model?
public View(IModel model)
{
_presenter = new Presenter(this, model);
}
private void SaveButton_Click(object sender, EventArgs e)
{
_presenter.Save();
}
private void CancelButton_Click(object sender, EventArgs e)
{
_presenter.Cancel();
}
public void UpdateModel(IModel newModel)
{
_presenter.UpdateModel(newModel);
}
}
编辑:正如下面评论中所述,我认为最好让视图完全愚蠢并且对演示者或模型一无所知。这是我修改后的代码。
模型:
public interface IModel
{
string CustomerName { get; set; }
}
public class Model : IModel
{
public string CustomerName { get; set; }
public Model(string name)
{
CustomerName = name;
}
}
看法:
public interface IView
{
event EventHandler SaveClick;
event EventHandler CancelClick;
string CustomerName { get; set; }
}
public class View : UserControl, IView
{
public event EventHandler SaveClick
{
add { SaveButton.Click += value; }
remove { SaveButton.Click -= value; }
}
public event EventHandler CancelClick
{
add { CancelButton.Click += value; }
remove { CancelButton.Click -= value; }
}
public string CustomerName
{
get { return CustomerNameEditBox.Text; }
set { CustomerNameEditBox.Text = value; }
}
}
主持人:
public class Presenter
{
private IView _view;
private IModel _model;
public Control View
{
get { return (Control)_view; }
}
public Presenter(IView view, IModel model)
{
_view = view;
_view.SaveClick += new EventHandler(_view_SaveClick);
_view.CancelClick += new EventHandler(_view_CancelClick);
_model = model;
_view.CustomerName = _model.CustomerName;
}
void _view_SaveClick(object sender, EventArgs e)
{
_model.CustomerName = _view.CustomerName;
}
void _view_CancelClick(object sender, EventArgs e)
{
_view.CustomerName = _model.CustomerName;
}
public void ChangeModel(IModel newModel)
{
_model = newModel;
_view.CustomerName = _model.CustomerName;
}
}
用法:
public class MainProgram : Form
{
public void ArbitraryMethod()
{
//Create a new model object
IModel model1 = new Model("test1");
//Create the presenter with the model and any view implementing IView
Presenter presenter = new Presenter(new View(), model1);
//Add the view to a panel control in this form
mainPanel.Controls.Add(presenter.View);
//Change the model
IModel model2 = new Model("test2");
presenter.ChangeModel(model2);
}
}