在使用 MVP 模式时,我经常遇到方法和成员似乎不太适合 View 或 Presenter 类...我的问题是:您使用什么规则来决定哪些类的功能是什么?我对 MVP 比较陌生,所以请幽默。
TIA。
在使用 MVP 模式时,我经常遇到方法和成员似乎不太适合 View 或 Presenter 类...我的问题是:您使用什么规则来决定哪些类的功能是什么?我对 MVP 比较陌生,所以请幽默。
TIA。
我倾向于支持 MVP 的被动视图变体,所以这对我来说不是问题。在被动视图模式中,视图几乎将任何比简单分配更复杂的事情委托给演示者。
你最终得到一个看起来像这样的模式:
public class MyView: IView
{
private MyPresenter Presenter;
private OnEvent()
{
Presenter.DoSomething();
}
public string MyProperty
{
get{ return UIControl.Property;}
set{ UIControl.Property = value}
}
}
public interface IView
{
public string MyProperty{ get; set;}
}
public class MyPresenter
{
private IView view;
public void DoSomething()
{
...
view.MyProperty = something;
}
}
唯一的技巧部分是如果您的表单上有一个数据网格。这些需要大量工作才能适应被动视图模式。
它归结为对 UI 进行了多少操作。如果该方法包含对单个控件的大量直接访问,那么它很可能属于演示者。否则它属于视图。目标是将视图和当前之间的交互减少到实现软件设计所需的最低限度。
例如
Presenter.SetListTitle MyList.Name
For I = View.MyListStart to View.MyListEnd
Presenter.AddListItem MyList(I)
Next I
Presenter.ShowListAddBUtton
Presenter.ShowListDelButton
应放置在演示者中,如下所示
Public Sub UpdateWithList(MyList as AList, View as AView)
Me.SetListTitle MyList.Name
For I = View.MyListStart to View.MyListEnd
Me.AddListItem MyList(I)
Next I
Me.ShowListAddBUtton
Me.ShowListDelButton
End Sub
稍后,如果您决定更改 UI,您只需担心实现 UpdateWithList 而不是 SetListTitle、AddListItem 等。