用C#实现MVC+观察者模式(WINFORM)
用C#實(shí)現(xiàn)MVC(Model View Control)模式介紹
益處
下載案例:https://download.csdn.net/download/gigizhongyan/13011332
??? 在我們的開發(fā)項(xiàng)目中使用MVC(Model-View-Control)模式的益處是,可以完全降低業(yè)務(wù)層和應(yīng)用表示層的相互影響。此外,我們會有完全獨(dú)立的對象來操作表示層。MVC在我們項(xiàng)目中提供的這種對象和層之間的獨(dú)立,將使我們的維護(hù)變得更簡單使我們的代碼重用變得很容易(下面你將看到)。
作為一般的習(xí)慣,我們知道我們希望保持最低的對象間的依賴,這樣變化能夠很容易的得到滿足,而且我們可以重復(fù)使用我們辛辛苦苦寫的代碼。為了達(dá)到這個目的我們將遵循一般的原則“對接口編成,而不是對類”來使用MVC模式。
我們的構(gòu)架概要
好,現(xiàn)在我們知道我們要使用MVC,我們需要指出它的本質(zhì)。通過我們的試驗(yàn)得出MVC的三個部分:Model,Control和View。在我們的系統(tǒng)中,Model就是我們的汽車,View就是我們的畫面,Control將這兩個部分聯(lián)系起來。
正在上傳…重新上傳取消
為了改變Model(我們的ACME 2000 sports car),我們需要使用Control。我們的Control將會產(chǎn)生給Model(我們的ACME 2000 sports car)的請求,和更新View,View就是我們的畫面(UI)。
這看起來很簡單,但是這里產(chǎn)生了第一個要解決的問題:當(dāng)終端用戶想做一個對ACME 2000 sports car一個改變將會發(fā)生什么,比如說加速或是轉(zhuǎn)向?他們將通過View(our windows form)用Control來提出一個變化的申請。
正在上傳…重新上傳取消
現(xiàn)在我們就剩下一個未解決問題了。如果View沒有必要的信息來顯示Model的狀態(tài)怎么辦?我們需要再在我們的圖中加入一個箭頭:View將能申請Model的狀態(tài)以便得到它要顯示的相關(guān)狀態(tài)信息。
正在上傳…重新上傳取消
最后,我們的最終用戶(司機(jī))將會和我們的ACME Vehicle Control系統(tǒng)通過View來交互。如果他們想發(fā)出一個改變系統(tǒng)的申請,比如提高一點(diǎn)加速度,申請將會從View開始發(fā)出由Control處理。
Control將會向Model申請改變并將必要的變化反映在View上。比如,如果一個蠻橫的司機(jī)對ACME 2000 Sports Car做了一個"floor it"申請,而現(xiàn)在行駛的太快不能轉(zhuǎn)向,那么Control將會拒絕這個申請并在View中通知,這樣就防止了在交通擁擠是發(fā)生悲慘的連環(huán)相撞。
Model?(the?ACME 2000 Sports Car) 將通知View?它的速度已經(jīng)提高,而View也將做適當(dāng)?shù)母隆?/span>
綜上,這就是我們將構(gòu)建的概要:
正在上傳…重新上傳取消
首先,我們考慮一下基本的項(xiàng)目。我們需要一些東西來表示方向和轉(zhuǎn)動請求。我們做了兩個枚舉類型:AbsoluteDirection 和 RelativeDirection
public enum AbsoluteDirection
{
North=0, East, South, West
}
public enum RelativeDirection
{
Right, Left, Back
}
下面來解決Control接口。我們知道Control需要將請求傳遞給Model,這些請求包括:Accelerate, Decelerate, 和 Turn。我們建立一個IVehicleControl接口,并加入適當(dāng)?shù)姆椒ā?/span>
public interface IVehicleControl
{
void Accelerate(int paramAmount);
void Decelerate(int paramAmount);
void Turn(RelativeDirection paramDirection);
}
現(xiàn)在我們來整理Model接口。我們需要知道汽車的名字,速度,最大速度,最大倒退速度,最大轉(zhuǎn)彎速度和方向。我們也需要加速,減速,轉(zhuǎn)彎的函數(shù)。
public interface IVehicleModel
{
string Name{ get; set;}
int Speed{ get; set;}
int MaxSpeed{ get;}
int MaxTurnSpeed{ get;}
int MaxReverseSpeed { get;}
AbsoluteDirection Direction{get; set;}
void Turn(RelativeDirection paramDirection);
void Accelerate(int paramAmount);
void Decelerate(int paramAmount);
}
最后,我們來整理View接口。我們知道View需要暴露出Control的一些機(jī)能,比如允許或禁止加速,減速和轉(zhuǎn)彎申請。
public interface IVehicleView
{
void DisableAcceleration();
void EnableAcceleration();
void DisableDeceleration();
void EnableDeceleration();
void DisableTurning();
void EnableTurning();
}
現(xiàn)在我們需要做一些微調(diào)使我們的這些接口能夠互相作用。首先,任何一個Control都需要知道它的View和Model,所以在我們的IvehicleControl接口中加入兩個函數(shù):"SetModel" 和"SetView":
public interface IVehicleControl
{
void RequestAccelerate(int paramAmount);
void RequestDecelerate(int paramAmount);
void RequestTurn(RelativeDirection paramDirection);
void SetModel(IVehicleModel paramAuto);
void SetView(IVehicleView paramView);
}
下一個部分比較巧妙。我們希望View知道Model中的變化。為了達(dá)到這個目的,我們使用觀察者模式。
為了實(shí)施觀察者模式,我們需要將下面的函數(shù)加入到Model(被View觀察):AddObserver, RemoveObserver, 和 NotifyObservers。
?
public interface IVehicleModel
{
string Name{ get; set;}
int Speed{ get; set;}
int MaxSpeed{ get;}
int MaxTurnSpeed{ get;}
int MaxReverseSpeed { get;}
AbsoluteDirection Direction{get; set;}
void Turn(RelativeDirection paramDirection);
void Accelerate(int paramAmount);
void Decelerate(int paramAmount);
void AddObserver(IVehicleView paramView);
void RemoveObserver(IVehicleView paramView);
void NotifyObservers();
}
并且將下面的函數(shù)加入到View(被Model觀察)中。這樣做的目的是Model會有一個View的引用。當(dāng)Model發(fā)生變化時,將會調(diào)用NotifyObservers()方法,傳入一個對其自身的引用并調(diào)用Update()通知View這個變化。
public class IVehicleView
{
void DisableAcceleration();
void EnableAcceleration();
void DisableDeceleration();
void EnableDeceleration();
void DisableTurning();
void EnableTurning();
void Update(IVehicleModel paramModel);
}
這樣我們就將我們的接口聯(lián)系起來了。在下面的代碼中我們只需要引用我們這些接口,這樣就保證了我們代碼的低耦合。任何顯示汽車狀態(tài)的用戶界面都需要實(shí)現(xiàn)IVehicleView,我們所有的ACME都需要實(shí)現(xiàn)IVehicleModel,并且我們需要為我們的ACME汽車制作Controls,這些Control將實(shí)現(xiàn)IVehicleControl接口。
我們知道所有的汽車都做相同的動作,所以我們接下來做一個基于“骨架”的共有的代碼來處理這些操作。這是一個抽象類,因?yàn)槲覀儾幌M魏稳嗽凇肮羌堋鄙祥_車(抽象類是不能被實(shí)例化的)。我們稱其為Automobile。我們將用一個ArrayList (from System.Collections)來保持跟蹤所有感興趣的Views(記住觀察者模式了嗎?)。我們也可以用老式的數(shù)組來記錄對IVehicleView的引用,但是現(xiàn)在我們已經(jīng)很累了想快點(diǎn)結(jié)束這篇文章。如果你感興趣,看一下在觀察者模式中AddObserver, RemoveObserver, 和NotifyObservers,這些函數(shù)是怎樣和IVehicleView互相作用的。任何時間當(dāng)有速度或方向變化時,Automobile通知所有的IVehicleViews。
?
public abstract class Automobile: IVehicleModel
{
"Declarations "#region "Declarations "
private ArrayList aList = new ArrayList();
private int mintSpeed = 0;
private int mintMaxSpeed = 0;
private int mintMaxTurnSpeed = 0;
private int mintMaxReverseSpeed = 0;
private AbsoluteDirection mDirection = AbsoluteDirection.North;
private string mstrName = "";
#endregion
"Constructor"#region "Constructor"
public Automobile(int paramMaxSpeed, int paramMaxTurnSpeed, int paramMaxReverseSpeed, string paramName)
{
this.mintMaxSpeed = paramMaxSpeed;
this.mintMaxTurnSpeed = paramMaxTurnSpeed;
this.mintMaxReverseSpeed = paramMaxReverseSpeed;
this.mstrName = paramName;
}
#endregion
"IVehicleModel Members"#region "IVehicleModel Members"
public void AddObserver(IVehicleView paramView)
{
aList.Add(paramView);
}
public void RemoveObserver(IVehicleView paramView)
{
aList.Remove(paramView);
}
public void NotifyObservers()
{
foreach(IVehicleView view in aList)
{
view.Update(this);
}
}
public string Name
{
get
{
return this.mstrName;
}
set
{
this.mstrName = value;
}
}
public int Speed
{
get
{
return this.mintSpeed;
}
}
public int MaxSpeed
{
get
{
return this.mintMaxSpeed;
}
}
public int MaxTurnSpeed
{
get
{
return this.mintMaxTurnSpeed;
}
}
public int MaxReverseSpeed
{
get
{
return this.mintMaxReverseSpeed;
}
}
public AbsoluteDirection Direction
{
get
{
return this.mDirection;
}
}
public void Turn(RelativeDirection paramDirection)
{
AbsoluteDirection newDirection;
switch(paramDirection)
{
case RelativeDirection.Right:
newDirection = (AbsoluteDirection)((int)(this.mDirection + 1) %4);
break;
case RelativeDirection.Left:
newDirection = (AbsoluteDirection)((int)(this.mDirection + 3) %4);
break;
case RelativeDirection.Back:
newDirection = (AbsoluteDirection)((int)(this.mDirection + 2) %4);
break;
default:
newDirection = AbsoluteDirection.North;
break;
}
this.mDirection = newDirection;
this.NotifyObservers();
}
public void Accelerate(int paramAmount)
{
this.mintSpeed += paramAmount;
if(mintSpeed >= this.mintMaxSpeed) mintSpeed = mintMaxSpeed;
this.NotifyObservers();
}
public void Decelerate(int paramAmount)
{
this.mintSpeed -= paramAmount;
if(mintSpeed <= this.mintMaxReverseSpeed) mintSpeed = mintMaxReverseSpeed;
this.NotifyObservers();
}
#endregion
}
最后但不是至少
現(xiàn)在我們的"ACME Framework"已經(jīng)做好了,我們只需要設(shè)立有形的類和接口。首先讓我們看看最后兩個類:Control 和 Model...
這里我們有形的AutomobileControl實(shí)現(xiàn)IVehicleControl接口。我們的AutomobileControl也將設(shè)置View來依賴Model 的狀態(tài)
注意,我們只是有對IVehicleModel的引用(而不是抽象類Automobile?)和對IVehicleView的引用(而不是具體的View),這樣保證對象間的低耦合。
?
public class AutomobileControl: IVehicleControl
{
private IVehicleModel Model;
private IVehicleView View;
public AutomobileControl(IVehicleModel paramModel, IVehicleView paramView)
{
this.Model = paramModel;
this.View = paramView;
}
public AutomobileControl()
{
}
IVehicleControl Members#region IVehicleControl Members
public void SetModel(IVehicleModel paramModel)
{
this.Model = paramModel;
}
public void SetView(IVehicleView paramView)
{
this.View = paramView;
}
public void RequestAccelerate(int paramAmount)
{
if(Model != null)
{
Model.Accelerate(paramAmount);
if(View != null) SetView();
}
}
public void RequestDecelerate(int paramAmount)
{
if(Model != null)
{
Model.Decelerate(paramAmount);
if(View != null) SetView();
}
}
public void RequestTurn(RelativeDirection paramDirection)
{
if(Model != null)
{
Model.Turn(paramDirection);
if(View != null) SetView();
}
}
#endregion
public void SetView()
{
if(Model.Speed >= Model.MaxSpeed)
{
View.DisableAcceleration();
View.EnableDeceleration();
}
else if(Model.Speed <= Model.MaxReverseSpeed)
{
View.DisableDeceleration();
View.EnableAcceleration();
}
else
{
View.EnableAcceleration();
View.EnableDeceleration();
}
if(Model.Speed >= Model.MaxTurnSpeed)
{
View.DisableTurning();
}
else
{
View.EnableTurning();
}
}
}
現(xiàn)在輪到我們的View了...
現(xiàn)在終于開始建立我們MVC最后一個部分了...View!
我們要建立一個AutoView來實(shí)現(xiàn)IVehicleView接口。這個AutoView將會有對Control和Model接口的引用。
public class AutoView : System.Windows.Forms.UserControl, IVehicleView
{
private IVehicleControl Control = new ACME.AutomobileControl();
private IVehicleModel Model = new ACME.ACME2000SportsCar("Speedy");
}
我們也需要將所有的東西包裝在UserControl的構(gòu)造函數(shù)中。
public AutoView()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
WireUp(Control, Model);
}
public void WireUp(IVehicleControl paramControl, IVehicleModel paramModel)
{
// If we're switching Models, don't keep watching
// the old one!
if(Model != null)
{
Model.RemoveObserver(this);
}
Model = paramModel;
Control = paramControl;
Control.SetModel(Model);
Control.SetView(this);
Model.AddObserver(this);
}
private void btnAccelerate_Click(object sender, System.EventArgs e)
{
Control.RequestAccelerate(int.Parse(this.txtAmount.Text));
}
private void btnDecelerate_Click(object sender, System.EventArgs e)
{
Control.RequestDecelerate(int.Parse(this.txtAmount.Text));
}
private void btnLeft_Click(object sender, System.EventArgs e)
{
Control.RequestTurn(RelativeDirection.Left);
}
private void btnRight_Click(object sender, System.EventArgs e)
{
Control.RequestTurn(RelativeDirection.Right);
}
public void UpdateInterface(IVehicleModel auto)
{
this.label1.Text = auto.Name + " heading " + auto.Direction.ToString() + " at speed: " + auto.Speed.ToString();
this.pBar.Value = (auto.Speed>0)? auto.Speed*100/auto.MaxSpeed : auto.Speed*100/auto.MaxReverseSpeed;
}
?
public void DisableAcceleration()
{
this.btnAccelerate.Enabled = false;
}
public void EnableAcceleration()
{
this.btnAccelerate.Enabled = true;
}
public void DisableDeceleration()
{
this.btnDecelerate.Enabled = false;
}
public void EnableDeceleration()
{
this.btnDecelerate.Enabled = true;
}
public void DisableTurning()
{
this.btnRight.Enabled = this.btnLeft.Enabled = false;
}
public void EnableTurning()
{
this.btnRight.Enabled = this.btnLeft.Enabled = true;
}
public void Update(IVehicleModel paramModel)
{
this.UpdateInterface(paramModel);
}
?
幸運(yùn)的是我們用的是MVC!我們需要做的所有工作就是建立一個新的ACMETruck類,包裝一下,完事!
?
public class ACME2000Truck: Automobile
{
public ACME2000Truck(string paramName):base(80, 25, -12, paramName){}
public ACME2000Truck(string paramName, int paramMaxSpeed, int paramMaxTurnSpeed, int paramMaxReverseSpeed):
base(paramMaxSpeed, paramMaxTurnSpeed, paramMaxReverseSpeed, paramName){}
}
在AutoView中,我們只需要建立卡車包裝一下!
private void btnBuildNew_Click(object sender, System.EventArgs e)
{
this.autoView1.WireUp(new ACME.AutomobileControl(), new ACME.ACME2000Truck(this.txtName.Text));
}
如果我們想要一個新Control只允許我們來每次加速或減速最大5mph,小意思!做一個SlowPokeControl(和我們的AutoControl相同,但是在申請加速度中做了限制)。
public void RequestAccelerate(int paramAmount)
{
if(Model != null)
{
int amount = paramAmount;
if(amount > 5) amount = 5;
Model.Accelerate(amount);
if(View != null) SetView();
}
}
public void RequestDecelerate(int paramAmount)
{
if(Model != null)
{
int amount = paramAmount;
if(amount > 5) amount = 5;
Model.Accelerate(amount);
Model.Decelerate(amount);
if(View != null) SetView();
}
}
如果我們想讓我們的ACME2000 Truck變得遲鈍,只需要在AutoView中包裝。
private void btnBuildNew_Click(object sender, System.EventArgs e)
{
this.autoView1.WireUp(new ACME.SlowPokeControl(), new ACME.ACME2000Truck(this.txtName.Text));
}
最后,如果我們需要一個在web上的接口,我們要做的所有工作就是建立一個Web項(xiàng)目在UserControl中實(shí)現(xiàn)IVehicleView接口。
結(jié)論
正如你所看到的,使用MVC來構(gòu)建代碼控制接口耦合性很低,很容易適應(yīng)需求的改變。它也能使變化的影響減小,而且你可以在任何地方重用你的虛函數(shù)和接口。有很多時候我們可以在我們的項(xiàng)目中實(shí)現(xiàn)伸縮性,特別是在那些需求變化的時候,但是這需要下次再說了。
?
?
總結(jié)
以上是生活随笔為你收集整理的用C#实现MVC+观察者模式(WINFORM)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 萧逸的武侠,我的回忆...
- 下一篇: 工程师的自我修养:全英文技术学习实践