C#实现MVC模式简要方法(3)
为了实施观察者模式,我们需要将下面的函数加入到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();
}
...并且将下面的函数加入到View(被Model观察)中。这样做的目的是Model会有一个View的引用。当Model发生变化时,将会调用NotifyObservers()方法,传入一个对其自身的引用并调用Update()通知View这个变化。
public class IVehicleView
{
void DisableAcceleration();
void EnableAcceleration();
void DisableDeceleration();
void EnableDeceleration();
void DisableTurning();
void EnableTurning();
void Update(IVehicleModel paramModel);
}
这样我们就将我们的接口联系起来了。在下面的代码中我们只需要引用我们这些接口,这样就保证了我们代码的低耦合。任何显示汽车状态的用户界面都需要实现IVehicleView,我们所有的ACME都需要实现IVehicleModel,并且我们需要为我们的ACME汽车制作Controls,这些Control将实现IVehicleControl接口。
下一步...在common中都需要什么
我们知道所有的汽车都做相同的动作,所以我们接下来做一个基于“骨架”的共有的代码来处理这些操作。这是一个抽象类,因为我们不希望任何人在“骨架”上开车(抽象类是不能被实例化的)。我们称其为Automobile。我们将用一个ArrayList (from System.Collections)来保持跟踪所有感兴趣的Views(记住观察者模式了吗?)。我们也可以用老式的数组来记录对IVehicleView的引用,但是现在我们已经很累了想快点结束这篇文章。如果你感兴趣,看一下在观察者模式中AddObserver, RemoveObserver, 和NotifyObservers,这些函数是怎样和IVehicleView互相作用的。任何时间当有速度或方向变化时,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
}
现在我们的"ACME Framework"已经做好了,我们只需要设立有形的类和接口。首先让我们看看最后两个类:Control 和 Model...
这里我们有形的AutomobileControl实现IVehicleControl接口。我们的AutomobileControl也将设置View来依赖Model 的状态(当有向Model的申请时检测SetView方法)。
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();
}
...并且将下面的函数加入到View(被Model观察)中。这样做的目的是Model会有一个View的引用。当Model发生变化时,将会调用NotifyObservers()方法,传入一个对其自身的引用并调用Update()通知View这个变化。
public class IVehicleView
{
void DisableAcceleration();
void EnableAcceleration();
void DisableDeceleration();
void EnableDeceleration();
void DisableTurning();
void EnableTurning();
void Update(IVehicleModel paramModel);
}
这样我们就将我们的接口联系起来了。在下面的代码中我们只需要引用我们这些接口,这样就保证了我们代码的低耦合。任何显示汽车状态的用户界面都需要实现IVehicleView,我们所有的ACME都需要实现IVehicleModel,并且我们需要为我们的ACME汽车制作Controls,这些Control将实现IVehicleControl接口。
下一步...在common中都需要什么
我们知道所有的汽车都做相同的动作,所以我们接下来做一个基于“骨架”的共有的代码来处理这些操作。这是一个抽象类,因为我们不希望任何人在“骨架”上开车(抽象类是不能被实例化的)。我们称其为Automobile。我们将用一个ArrayList (from System.Collections)来保持跟踪所有感兴趣的Views(记住观察者模式了吗?)。我们也可以用老式的数组来记录对IVehicleView的引用,但是现在我们已经很累了想快点结束这篇文章。如果你感兴趣,看一下在观察者模式中AddObserver, RemoveObserver, 和NotifyObservers,这些函数是怎样和IVehicleView互相作用的。任何时间当有速度或方向变化时,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
}
现在我们的"ACME Framework"已经做好了,我们只需要设立有形的类和接口。首先让我们看看最后两个类:Control 和 Model...
这里我们有形的AutomobileControl实现IVehicleControl接口。我们的AutomobileControl也将设置View来依赖Model 的状态(当有向Model的申请时检测SetView方法)。