C#实现MVC模式简要方法(5)

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;
}

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

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类,包装一下,完事!

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项目在UserControl中实现IVehicleView接口。

  结论

  正如你所看到的,使用MVC来构建代码控制接口耦合性很低,很容易适应需求的改变。它也能使变化的影响减小,而且你可以在任何地方重用你的虚函数和接口。有很多时候我们可以在我们的项目中实现伸缩性,特别是在那些需求变化的时候,但是这需要下次再说了。
posted on 2009-08-27 22:32  刘宁Toby  阅读(318)  评论(0编辑  收藏  举报