Visual C#实现MVC模式简要方法

飞雪彗

飞雪彗

2016-02-19 14:40

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享Visual C#实现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人的费力咨询,我们得到了很多有关详细设计的信息。我们想确定我们可以设置的最

  大速度在前进,后退和转弯中。我们也需要能够加速,减速,左转和右转。我们的仪表盘必须显示当前的速度和方向。

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

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

123456789 AbsoluteDirection 和 RelativeDirection。 public enum AbsoluteDirection { North=0, East, South, West } public enum RelativeDirection { Right, Left, Back }

  下面来解决Control接口。我们知道Control需要将请求传递给Model,这些请求包括:Accelerate, Decelerate,

和 Turn。我们建立一个IVehicleControl接口,并加入适当的方法。

123456 public interface IVehicleControl { void Accelerate(int paramAmount); void Decelerate(int paramAmount); void Turn(RelativeDirection paramDirection); }

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

  我们也需要加速,减速,转弯的函数。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/) 123456789101112 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的一些机能,比如允许或禁止加速,减速和转弯申请。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/) 123456789 public interface IVehicleView { void DisableAcceleration(); void EnableAcceleration(); void DisableDeceleration(); void EnableDeceleration(); void DisableTurning(); void EnableTurning(); }

  现在我们需要做一些微调使我们的这些接口能够互相作用。首先,任何一个Control都需要知道它的View和Model,

  所以在我们的IvehicleControl接口中加入两个函数:"SetModel" 和"SetView":

12345678 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中的变化。为了达到这个目的,我们使用观察者模式。

  开始

  作为总是想的远一点的开发人员,我们想让我们的系统有一个长久并且良好的生命周期。这就是说能够进可能的

  准备好满足ACME的很多变化。为了做到这一点,我们知道要遵循两条原则...保证你的类低耦合,要达到这个

  目标,还要对接口编程。

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

  经过很多调查研究,和与ACME人的费力咨询,我们得到了很多有关详细设计的信息。我们想确定我们可以设置的

  最大速度在前进,后退和转弯中。我们也需要能够加速,减速,左转和右转。我们的仪表盘必须显示当前的速度和方向。

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

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

  AbsoluteDirection 和 RelativeDirection。

12345678 public enum AbsoluteDirection { North=0, East, South, West } public enum RelativeDirection { Right, Left, Back }

  下面来解决Control接口。我们知道Control需要将请求传递给Model,这些请求包括:Accelerate, Decelerate,

  和 Turn。我们建立一个IVehicleControl接口,并加入适当的方法。

123456 public interface IVehicleControl { void Accelerate(int paramAmount); void Decelerate(int paramAmount); void Turn(RelativeDirection paramDirection); }

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

 

  我们也需要加速,减速,转弯的函数。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  

123456789101112 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的一些机能,比如允许或禁止加速,减速和转弯申请。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/) 123456789 public interface IVehicleView { void DisableAcceleration(); void EnableAcceleration(); void DisableDeceleration(); void EnableDeceleration(); void DisableTurning(); void EnableTurning(); }

  现在我们需要做一些微调使我们的这些接口能够互相作用。首先,任何一个Control都需要知道它的View和Model,

所以在我们的IvehicleControl接口中加入两个函数:"SetModel" 和"SetView":

12345678 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中的变化。为了达到这个目的,我们使用观察者模式。

  注意,我们只是有对IVehicleModel的引用(而不是抽象类Automobile )和对IVehicleView的引用(而不是具体的View),

  这样保证对象间的低耦合。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 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接口):

1234567 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接口的引用。

  

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

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

1234567891011121314151617181920 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来显示编码。

 

  

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

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

123456 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接口的方法。

展开更多 50%)
分享

猜你喜欢

Visual C#实现MVC模式简要方法

编程语言 网络编程
Visual C#实现MVC模式简要方法

Visual C#实现Windows信使服务

编程语言 网络编程
Visual C#实现Windows信使服务

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

用Visual C#实现文件下载功能

编程语言 网络编程
用Visual C#实现文件下载功能

利用Visual C#实现Window管道技术

编程语言 网络编程
利用Visual C#实现Window管道技术

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Visual C#使用DirectX实现视频播放

编程语言 网络编程
Visual C#使用DirectX实现视频播放

利用Visual C#实现ICMP网络协议

编程语言 网络编程
利用Visual C#实现ICMP网络协议

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Javascript正则表达式详解

Javascript正则表达式详解

JavaScript实例教程(四)鼠标事件和浏览器的提示栏及IF语句的运

JavaScript实例教程(四)鼠标事件和浏览器的提示栏及IF语句的运
下拉加载更多内容 ↓