(編輯很多)我已經得到了一些類與文摘成員。抽象成員的具體類型應根據用戶的輸入在課堂實例中確定。但是,第二個成員的具體類型可能取決於第一個成員。使用MVP的複合結構的對象模型
我試圖做一些事情保持MVP設計模式的初衷。我教過如何讓Presenter將代表傳遞給Model的Ctor,Ctor將用它來請求實現該類實例所需的信息。我不確定這是個好主意。這是我寫的:
// In the Model :
public class Model
{
public E Element1;
public E Element2;
public Model(CustomCtor<ModelElement, IModelElement> GetModelElement)
{
this.Element1 = (E)GetModelElement(ModelElement.E, null);
this.Element2 = (E)GetModelElement(ModelElement.E, null);
//Element2 does not depend on Element1 in this case though.
}
}
public abstract class E : IModelElement { }
public class EA : E
{
public string Element1;
public EA(string Element1) { this.Element1 = Element1; }
}
public class EB : E
{
public int Element1;
public EB(int Element1) { this.Element1 = Element1; }
}
public interface IModelElement { }
public enum ModelElement { E, EA, EB }
// In the Presenter :
public class Presenter
{
View.View view1;
public Presenter() { }
public void SetView(View.View view) { this.view1 = view; }
public Model.Model MakeModel()
{
CustomCtor<ModelElement, IModelElement> GetModelElement = new CustomCtor<ModelElement, IModelElement>(GetModelElement<ModelElement, IModelElement>);
return new Model.Model(GetModelElement);
}
private Model.IModelElement GetModelElement<ModelElement, Tout>(Model.ModelElement ME, object obj)
{
switch (ME)
{
case Model.ModelElement.E:
return MakeE();
// One case per Model.ModelElement
default:
throw new Exception("ModelElement not implemented in the Presenter.");
}
return default(Model.IModelElement);
}
private E MakeE()
{
switch (view1.AskEType())
{
case 1:
return MakeEA();
case 2:
return MakeEB();
default:
throw new Exception();
}
}
private EA MakeEA() { return new EA(view1.AskString("EA.Element1 (String)")); }
private EB MakeEB() { return new EB(view1.AskInt("EB.Element1 (Int)")); }
}
// Shared to the Model and the Presenter :
public delegate TOut CustomCtor<EnumType, TOut>(EnumType Enum, object Params) where EnumType : struct;
// In the View :
public class View
{
public int AskEType()
{
Console.WriteLine(string.Format("Type of E : EA(1) or EB(2)?"));
return int.Parse(Console.ReadLine());
}
public string AskString(string Name)
{
Console.Write(string.Format("{0} ? ", Name));
return Console.ReadLine();
}
public int AskInt(string Name)
{
Console.Write(string.Format("{0} ? ", Name));
return int.Parse(Console.ReadLine());
}
}
//In the Program :
class Program
{
static void Main(string[] args)
{
View.View view1 = new View.View();
Presenter.Presenter presenter1 = new Presenter.Presenter();
presenter1.SetView(view1);
presenter1.MakeModel();
}
}
這有道理嗎?有沒有我想要做的事情的名字? (旁邊的「一個奇怪的事情」) 你知道我應該閱讀的設計模式嗎? 我教過關於將Builder設計模式與MVP混合使用,但我不知道該怎麼做。
感謝
我用Viewer作爲控制檯編寫了這段代碼,但實際上我打算使用「IView」,因爲真正的視圖將是WinForm和WebForm。另外,輸入會稍微複雜一些(主要是下拉菜單和數字文本框),所以我打算爲每個組件類型製作一些用戶控件。我還計劃讓Presenter直接從視圖中讀取信息(而不是使視圖返回值)。考慮到附加信息,你是否仍然建議我使用MVC而不是MCP?感謝您的意見,非常感謝。 – Tipx 2009-09-01 02:51:28
使用WebForms,MVP會很有幫助。按照原樣創建WebForms的MVC是很困難的。 MVP也可以使用Windows窗體,因爲WinForms和WebForms的工作方式非常相似。但是,這並沒有真正改變畫面。這是非常重要的,這個觀點是「愚蠢的」,並且演示者處理輸入。主持人不應該問任何問題,因爲輸入應該先通過主持人,而不是其他方式。 IView的實現應該讓主持人有能力將數據推送到視圖中,而不是其他的。 – jrista 2009-09-01 03:16:35
謝謝,我會記住這一點,而我會嘗試一些事情。我在編程方面有着無人能及的地位,我還沒有看到太多的設計模式,但我毫不懷疑你的建議會讓我避免一些陷阱! (即使如此,我會犯錯誤,但會向他們學習。)謝謝。 – Tipx 2009-09-01 03:30:52