mvc——为项目提速

益处

    在我们的开发项目中使用MVC(Model-View-Control)模式的益处是,可以完全降低业务层和应用表示层的相互影响。此外,我们会有完全独立的对象来操作表示层。MVC在我们项目中提供的这种对象和层之间的独立,将使我们的维护变得更简单使我们的代码重用变得很容易(下面你将看到)。

    作为一般的习惯,我们知道我们希望保持最低的对象间的依赖,这样变化能够很容易的得到满足,而且我们可以重复使用我们辛辛苦苦写的代码。为了达到这个目的我们将遵循一般的原则“对接口编成,而不是对类”来使用MVC模式。

我们的使命,如果我们选择接受它...

    我们被委任构建一个ACME 2000 Sports Car项目,我们的任务是做一个简单的Windows画面来显示汽车的方向和速度,使终端用户能够改变方向,加速或是减速。当然将会有范围的扩展。

    在ACME已经有了传言,如果我们的项目成功,我们最终还要为ACME 2 Pickup Truck 和ACME 1 Tricycle开发一个相似的接口。作为开发人员,我们也知道ACME管理团队最终将问“这样是很棒的,我们能够在我们的intranet上看到它?”所有的这些浮现在脑海中,我们想交付一个产品,使它能够容易的升级以便能够保证将来我们能够有饭吃。

    所以,同时我们决定“这是使用MVC的一个绝好情形”

我们的构架概要

    好,现在我们知道我们要使用MVC,我们需要指出它的本质。通过我们的试验得出MVC的三个部分:Model,Control和View。在我们的系统中,Model就是我们的汽车,View就是我们的画面,Control将这两个部分联系起来。

 

    为了改变Model(我们的ACME 2000 sports car),我们需要使用Control。我们的Control将会产生给Model(我们的ACME 2000 sports car)的请求,和更新View,View就是我们的画面(UI)。

    这看起来很简单,但是这里产生了第一个要解决的问题:当终端用户想做一个对ACME 2000 sports car一个改变将会发生什么,比如说加速或是转向?他们将通过View(our windows form)用Control来提出一个变化的申请。

 

    现在我们就剩下一个未解决问题了。如果View没有必要的信息来显示Model的状态怎么办?我们需要再在我们的图中加入一个箭头:View将能申请Model的状态以便得到它要显示的相关状态信息。

 

    最后,我们的最终用户(司机)将会和我们的ACME Vehicle Control系统通过View来交互。如果他们想发出一个改变系统的申请,比如提高一点加速度,申请将会从View开始发出由Control处理。

    Control将会向Model申请改变并将必要的变化反映在View上。比如,如果一个蛮横的司机对ACME 2000 Sports Car做了一个"floor it"申请,而现在行驶的太快不能转向,那么Control将会拒绝这个申请并在View中通知,这样就防止了在交通拥挤是发生悲惨的连环相撞。

    Model (the ACME 2000 Sports Car) 将通知View 它的速度已经提高,而View也将做适当的更新。

    综上,这就是我们将构建的概要:


开始:

    作为总是想的远一点的开发人员,我们想让我们的系统有一个长久并且良好的生命周期。这就是说能够进可能的准备好满足ACME的很多变化。为了做到这一点,我们知道要遵循两条原则...“保证你的类低耦合”,要达到这个目标,还要“对接口编程”。

    所以我们要做三个接口(正如你所猜测,一个Model接口,一个View接口,一个Control接口)。 

    经过很多调查研究,和与ACME人的费力咨询,我们得到了很多有关详细设计的信息。我们想确定我们可以设置的最大速度在前进,后退和转弯中。我们也需要能够加速,减速,左转和右转。我们的仪表盘必须显示当前的速度和方向。

    实现所有这些需求是非常苛刻的,但是我们确信我们能够做到...

    首先,我们考虑一下基本的项目。我们需要一些东西来表示方向和转动请求。我们做了两个枚举类型:AbsoluteDirection 和 RelativeDirection。

  1. public enum AbsoluteDirection  
  2. {  
  3.         North=0, East, South, West  
  4. }  
  5. public enum RelativeDirection  
  6. {  
  7.          Right, Left, Back  
  8. }  
public enum AbsoluteDirection
{
        North=0, East, South, West
}
public enum RelativeDirection
{
         Right, Left, Back
}

    下面来解决Control接口。我们知道Control需要将请求传递给Model,这些请求包括:Accelerate, Decelerate, 和 Turn。我们建立一个IVehicleControl接口,并加入适当的方法。

  1. public interface IVehicleControl  
  2. {  
  3. void Accelerate(int paramAmount);  
  4. void Decelerate(int paramAmount);  
  5. void Turn(RelativeDirection paramDirection);   
  6. }  
public interface IVehicleControl
{
void Accelerate(int paramAmount);
void Decelerate(int paramAmount);
void Turn(RelativeDirection paramDirection); 
}

    现在我们来整理Model接口。我们需要知道汽车的名字,速度,最大速度,最大倒退速度,最大转弯速度和方向。我们也需要加速,减速,转弯的函数。

  1. public interface IVehicleModel  
  2. {  
  3. string Name{ getset;}  
  4. int Speed{ getset;}  
  5. int MaxSpeed{ get;}  
  6. int MaxTurnSpeed{ get;}  
  7. int MaxReverseSpeed { get;}  
  8. AbsoluteDirection Direction{getset;}  
  9. void Turn(RelativeDirection paramDirection);  
  10. void Accelerate(int paramAmount);  
  11. void Decelerate(int paramAmount);  
  12. }  
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的一些机能,比如允许或禁止加速,减速和转弯申请。

  1. public interface IVehicleView  
  2. {  
  3. void DisableAcceleration();  
  4. void EnableAcceleration();  
  5. void DisableDeceleration();  
  6. void EnableDeceleration();  
  7. void DisableTurning();  
  8. void EnableTurning();  
  9. }  
public interface IVehicleView
{
void DisableAcceleration();
void EnableAcceleration();
void DisableDeceleration();
void EnableDeceleration();
void DisableTurning();
void EnableTurning();
}



 

    现在我们需要做一些微调使我们的这些接口能够互相作用。首先,任何一个Control都需要知道它的View和Model,所以在我们的IvehicleControl接口中加入两个函数:"SetModel" 和"SetView":

  1. public interface IVehicleControl  
  2. {  
  3. void RequestAccelerate(int paramAmount);  
  4. void RequestDecelerate(int paramAmount);  
  5. void RequestTurn(RelativeDirection paramDirection);   
  6. void SetModel(IVehicleModel paramAuto);  
  7. void SetView(IVehicleView paramView);  
  8. }  
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中的变化。为了达到这个目的,我们使用观察者模式。

    为了实施观察者模式,我们需要将下面的函数加入到Model(被View观察):AddObserver, RemoveObserver, 和 NotifyObservers。

  1. public interface IVehicleModel  
  2. {  
  3. string Name{ getset;}  
  4. int Speed{ getset;}  
  5. int MaxSpeed{ get;}  
  6. int MaxTurnSpeed{ get;}  
  7. int MaxReverseSpeed { get;}  
  8. AbsoluteDirection Direction{getset;}  
  9. void Turn(RelativeDirection paramDirection);  
  10. void Accelerate(int paramAmount);  
  11. void Decelerate(int paramAmount);  
  12. void AddObserver(IVehicleView paramView);  
  13. void RemoveObserver(IVehicleView paramView);  
  14. void NotifyObservers();  
  15. }    
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这个变化。

  1. public class IVehicleView  
  2. {  
  3. void DisableAcceleration();  
  4. void EnableAcceleration();  
  5. void DisableDeceleration();  
  6. void EnableDeceleration();  
  7. void DisableTurning();  
  8. void EnableTurning();  
  9. void Update(IVehicleModel paramModel);  
  10. }  
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。

  1. public abstract class Automobile: IVehicleModel  
  2. {  
  3. "Declarations "#region "Declarations "  
  4. private ArrayList aList = new ArrayList();  
  5. private int mintSpeed = 0;  
  6. private int mintMaxSpeed = 0;  
  7. private int mintMaxTurnSpeed = 0;  
  8. private int mintMaxReverseSpeed = 0;  
  9. private AbsoluteDirection mDirection = AbsoluteDirection.North;  
  10. private string mstrName = ""; 
  11. #endregion   
  12. "Constructor"#region "Constructor"  
  13. public Automobile(int paramMaxSpeed, int paramMaxTurnSpeed, int paramMaxReverseSpeed, string paramName)  
  14. {  
  15. this.mintMaxSpeed = paramMaxSpeed;  
  16. this.mintMaxTurnSpeed = paramMaxTurnSpeed;  
  17. this.mintMaxReverseSpeed = paramMaxReverseSpeed;  
  18. this.mstrName = paramName;  
  19. }  
  20. #endregion   
  21. "IVehicleModel Members"#region "IVehicleModel Members"  
  22. public void AddObserver(IVehicleView paramView)  
  23. {  
  24. aList.Add(paramView);  
  25. }  
  26. public void RemoveObserver(IVehicleView paramView)  
  27. {  
  28. aList.Remove(paramView);  
  29. }  
  30. public void NotifyObservers(){  
  31. foreach(IVehicleView view in aList)  
  32. {  
  33. view.Update(this);  
  34. }  
  35. }  
  36. public string Name  
  37. {  
  38. get  
  39. {  
  40. return this.mstrName;  
  41. }  
  42. set  
  43. {  
  44. this.mstrName = value;  
  45. }  
  46. }  
  47. public int Speed  
  48. {  
  49. get  
  50. {  
  51. return this.mintSpeed;  
  52. }  
  53. }  
  54. public int MaxSpeed  
  55. {  
  56. get  
  57. {  
  58. return this.mintMaxSpeed;  
  59. }  
  60. }  
  61. public int MaxTurnSpeed  
  62. {  
  63. get  
  64. {  
  65. return this.mintMaxTurnSpeed;  
  66. }  
  67. }  
  68. public int MaxReverseSpeed  
  69. {  
  70. get  
  71. {  
  72. return this.mintMaxReverseSpeed;  
  73. }  
  74. }  
  75. public AbsoluteDirection Direction  
  76. {  
  77. get  
  78. {  
  79. return this.mDirection;  
  80. }  
  81. }  
  82. public void Turn(RelativeDirection paramDirection)  
  83. {  
  84. AbsoluteDirection newDirection;  
  85. switch(paramDirection)  
  86. {  
  87. case RelativeDirection.Right:  
  88. newDirection = (AbsoluteDirection)((int)(this.mDirection + 1) %4);  
  89. break;  
  90. case RelativeDirection.Left:  
  91. newDirection = (AbsoluteDirection)((int)(this.mDirection + 3) %4);  
  92. break;  
  93. case RelativeDirection.Back:  
  94. newDirection = (AbsoluteDirection)((int)(this.mDirection + 2) %4);  
  95. break;  
  96. default:  
  97. newDirection = AbsoluteDirection.North;  
  98. break;  
  99. }  
  100. this.mDirection = newDirection;  
  101. this.NotifyObservers();  
  102. }  
  103. public void Accelerate(int paramAmount)  
  104. {  
  105. this.mintSpeed += paramAmount;  
  106. if(mintSpeed >= this.mintMaxSpeed) mintSpeed = mintMaxSpeed;  
  107. this.NotifyObservers();  
  108. }  
  109. public void Decelerate(int paramAmount)  
  110. {  
  111. this.mintSpeed -= paramAmount;  
  112. if(mintSpeed <= this.mintMaxReverseSpeed) mintSpeed = mintMaxReverseSpeed;  
  113. this.NotifyObservers();  
  114. }  
  115. #endregion   
  116. }  
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"已经做好了,我们只需要设立有形的类和接口。首先让我们看看最后两个类:ControlModel...

    这里我们有形的AutomobileControl实现IVehicleControl接口。我们的AutomobileControl也将设置View来依赖Model 的状态(当有向Model的申请时检测SetView方法)。

    注意,我们只是有对IVehicleModel的引用(而不是抽象类Automobile )和对IVehicleView的引用(而不是具体的View),这样保证对象间的低耦合。

  1. public class AutomobileControl: IVehicleControl  
  2. {  
  3. private IVehicleModel Model;  
  4. private IVehicleView View;  
  5. public AutomobileControl(IVehicleModel paramModel, IVehicleView paramView)  
  6. {  
  7. this.Model = paramModel;  
  8. this.View = paramView;  
  9. }  
  10. public AutomobileControl()  
  11. {  
  12. }  
  13. IVehicleControl Members#region IVehicleControl Members  
  14. public void SetModel(IVehicleModel paramModel)  
  15. {  
  16. this.Model = paramModel;  
  17. }  
  18. public void SetView(IVehicleView paramView)  
  19. {  
  20. this.View = paramView;  
  21. }  
  22. public void RequestAccelerate(int paramAmount)  
  23. {  
  24. if(Model != null)  
  25. {  
  26. Model.Accelerate(paramAmount);  
  27. if(View != null) SetView();  
  28. }  
  29. }  
  30. public void RequestDecelerate(int paramAmount)  
  31. {  
  32. if(Model != null)  
  33. {  
  34. Model.Decelerate(paramAmount);  
  35. if(View != null) SetView();  
  36. }  
  37. }  
  38. public void RequestTurn(RelativeDirection paramDirection)  
  39. {  
  40. if(Model != null)  
  41. {  
  42. Model.Turn(paramDirection);  
  43. if(View != null) SetView();  
  44. }  
  45. }  
  46. #endregion   
  47. public void SetView()  
  48. {  
  49. if(Model.Speed >= Model.MaxSpeed)  
  50. {  
  51. View.DisableAcceleration();  
  52. View.EnableDeceleration();  
  53. }  
  54. else if(Model.Speed <= Model.MaxReverseSpeed)  
  55. {  
  56. View.DisableDeceleration();  
  57. View.EnableAcceleration();  
  58. }  
  59. else  
  60. {  
  61. View.EnableAcceleration();  
  62. View.EnableDeceleration();  
  63. }  
  64. if(Model.Speed >= Model.MaxTurnSpeed)  
  65. {  
  66. View.DisableTurning();  
  67. }  
  68. else  
  69. {  
  70. View.EnableTurning();  
  71. }  
  72. }  
  73. }  
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();
}
}
}



 

    这里是我们的ACME200SportsCar类(从抽象类Automobile继承,实现了IVehicleModel接口):

  1. public class ACME2000SportsCar:Automobile  
  2. {  
  3. public ACME2000SportsCar(string paramName):base(250, 40, -20, paramName){}  
  4. public ACME2000SportsCar(string paramName, int paramMaxSpeed, int paramMaxTurnSpeed, int paramMaxReverseSpeed):  
  5. base(paramMaxSpeed, paramMaxTurnSpeed, paramMaxReverseSpeed, paramName){}  
  6. }  
public class ACME2000SportsCar:Automobile
{
public ACME2000SportsCar(string paramName):base(250, 40, -20, paramName){}
public ACME2000SportsCar(string paramName, int paramMaxSpeed, int paramMaxTurnSpeed, int paramMaxReverseSpeed):
base(paramMaxSpeed, paramMaxTurnSpeed, paramMaxReverseSpeed, paramName){}
}


 

现在轮到我们的View了...

    现在终于开始建立我们MVC最后一个部分了...View!

    我们要建立一个AutoView来实现IVehicleView接口。这个AutoView将会有对Control和Model接口的引用。

  1. public class AutoView : System.Windows.Forms.UserControl, IVehicleView  
  2. {   
  3. private IVehicleControl Control = new ACME.AutomobileControl();   
  4. private IVehicleModel Model = new ACME.ACME2000SportsCar("Speedy");  
  5. }   
public class AutoView : System.Windows.Forms.UserControl, IVehicleView
{ 
private IVehicleControl Control = new ACME.AutomobileControl(); 
private IVehicleModel Model = new ACME.ACME2000SportsCar("Speedy");
} 

      我们也需要将所有的东西包装在UserControl的构造函数中。

  1. public AutoView()  
  2. {  
  3. // This call is required by the Windows.Forms Form Designer.  
  4. InitializeComponent();  
  5. WireUp(Control, Model);  
  6. }  
  7. public void WireUp(IVehicleControl paramControl, IVehicleModel paramModel)  
  8. {  
  9. // If we're switching Models, don't keep watching  
  10. // the old one!    
  11. if(Model != null)  
  12. {  
  13. Model.RemoveObserver(this);  
  14. }  
  15. Model = paramModel;  
  16. Control = paramControl;  
  17. Control.SetModel(Model);  
  18. Control.SetView(this);  
  19. Model.AddObserver(this);  
  20. }  
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);
}


 

      下面,加入我们的Button和一个label来显示ACME2000 Sports Car的状态还有状态条用来为所有的Buttons来显示编码。

  1. private void btnAccelerate_Click(object sender, System.EventArgs e)  
  2. {  
  3. Control.RequestAccelerate(int.Parse(this.txtAmount.Text));  
  4. }  
  5. private void btnDecelerate_Click(object sender, System.EventArgs e)  
  6. {  
  7. Control.RequestDecelerate(int.Parse(this.txtAmount.Text));  
  8. }  
  9. private void btnLeft_Click(object sender, System.EventArgs e)  
  10. {  
  11. Control.RequestTurn(RelativeDirection.Left);  
  12. }  
  13. private void btnRight_Click(object sender, System.EventArgs e)  
  14. {  
  15. Control.RequestTurn(RelativeDirection.Right);  
  16. }  
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);
}


 

      加入一个方法来更新接口...

  1. public void UpdateInterface(IVehicleModel auto)  
  2. {  
  3. this.label1.Text = auto.Name + " heading " + auto.Direction.ToString() + " at speed: " + auto.Speed.ToString();  
  4. this.pBar.Value = (auto.Speed>0)? auto.Speed*100/auto.MaxSpeed : auto.Speed*100/auto.MaxReverseSpeed;  
  5. }  
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;
}


 

     最后我们实现IVehicleView接口的方法。

  1. public void DisableAcceleration()  
  2. {  
  3. this.btnAccelerate.Enabled = false;  
  4. }  
  5. public void EnableAcceleration()  
  6. {  
  7. this.btnAccelerate.Enabled = true;  
  8. }  
  9. public void DisableDeceleration()  
  10. {  
  11. this.btnDecelerate.Enabled = false;  
  12. }  
  13. public void EnableDeceleration()  
  14. {  
  15. this.btnDecelerate.Enabled = true;  
  16. }  
  17. public void DisableTurning()  
  18. {  
  19. this.btnRight.Enabled = this.btnLeft.Enabled = false;  
  20. }  
  21. public void EnableTurning()  
  22. {  
  23. this.btnRight.Enabled = this.btnLeft.Enabled = true;  
  24. }  
  25. public void Update(IVehicleModel paramModel)  
  26. {  
  27. this.UpdateInterface(paramModel);  
  28. }  
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);
}


 

我们终于结束了!!!

       现在我们可以来测试ACME2000 Sports Car了。一切按计划进行,然后我们找到ACME的主管人员,但他想要开一个载货卡车而不是运动车。

       幸运的是我们用的是MVC!我们需要做的所有工作就是建立一个新的ACMETruck类,包装一下,完事!

  1. public class ACME2000Truck: Automobile  
  2. {  
  3. public ACME2000Truck(string paramName):base(80, 25, -12, paramName){}  
  4. public ACME2000Truck(string paramName, int paramMaxSpeed, int paramMaxTurnSpeed, int paramMaxReverseSpeed):  
  5. base(paramMaxSpeed, paramMaxTurnSpeed, paramMaxReverseSpeed, paramName){}   
  6. }  
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中,我们只需要建立卡车包装一下!

  1. private void btnBuildNew_Click(object sender, System.EventArgs e)  
  2. {  
  3. this.autoView1.WireUp(new ACME.AutomobileControl(), new ACME.ACME2000Truck(this.txtName.Text));  
  4. }  
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相同,但是在申请加速度中做了限制)。

  1. public void RequestAccelerate(int paramAmount)  
  2. {  
  3. if(Model != null)  
  4. {  
  5. int amount = paramAmount;  
  6. if(amount > 5) amount = 5;  
  7. Model.Accelerate(amount);  
  8. if(View != null) SetView();  
  9. }  
  10. }  
  11. public void RequestDecelerate(int paramAmount)  
  12. {  
  13. if(Model != null)  
  14. {  
  15. int amount = paramAmount;  
  16. if(amount > 5) amount = 5;  
  17. Model.Accelerate(amount);  
  18. Model.Decelerate(amount);  
  19. if(View != null) SetView();  
  20. }  
  21. }  
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中包装。

  1. private void btnBuildNew_Click(object sender, System.EventArgs e)  
  2. {  
  3. this.autoView1.WireUp(new ACME.SlowPokeControl(), new ACME.ACME2000Truck(this.txtName.Text));  
  4. }  
private void btnBuildNew_Click(object sender, System.EventArgs e)
{
this.autoView1.WireUp(new ACME.SlowPokeControl(), new ACME.ACME2000Truck(this.txtName.Text));
}

      最后,如果我们需要一个在web上的接口,我们要做的所有工作就是建立一个Web项目在UserControl中实现IVehicleView接口。
 

结论

        正如你所看到的,使用MVC来构建代码控制接口耦合性很低,很容易适应需求的改变。它也能使变化的影响减小,而且你可以在任何地方重用你的虚函数和接口。有很多时候我们可以在我们的项目中实现伸缩性,特别是在那些需求变化的时候,但是这需要下次再说了。

        于此同时,做下一个项目的时候记住MVC...你不会感到遗憾!

        Happy Driving.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值