用C#实现MVC(Model View Control)模式介绍

益处

下载案例:https://download.csdn.net/download/gigizhongyan/13011332

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

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

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

正在上传…重新上传取消

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

public enum AbsoluteDirection

{

North=0, East, South, West

}

public enum RelativeDirection

{

Right, Left, Back

}

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

public interface IVehicleControl

{

void Accelerate(int paramAmount);

void Decelerate(int paramAmount);

void Turn(RelativeDirection paramDirection);

}

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

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

public interface IVehicleView

{

void DisableAcceleration();

void EnableAcceleration();

void DisableDeceleration();

void EnableDeceleration();

void DisableTurning();

void EnableTurning();

}

现在我们需要做一些微调使我们的这些接口能够互相作用。首先,任何一个Control都需要知道它的View和Model,所以在我们的IvehicleControl接口中加入两个函数:"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中的变化。为了达到这个目的,我们使用观察者模式。

为了实施观察者模式,我们需要将下面的函数加入到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接口。

我们知道所有的汽车都做相同的动作,所以我们接下来做一个基于“骨架”的共有的代码来处理这些操作。这是一个抽象类,因为我们不希望任何人在“骨架”上开车(抽象类是不能被实例化的)。我们称其为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 的状态

注意,我们只是有对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();

}

}

}

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

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

我们要建立一个AutoView来实现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的构造函数中。

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

}

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

用C#实现MVC+观察者模式(WINFORM)相关推荐

  1. 【WEB API项目实战干货系列】- API访问客户端(WebApiClient适用于MVC/WebForms/WinForm)(四)

    目前最新的代码已经通过Sqlite + NHibernate + Autofac满足了我们基本的Demo需求. 按照既定的要求,我们的API会提供给众多的客户端使用, 这些客户端可以是各种Web站点, ...

  2. YbSoftwareFactory 代码生成插件【九】:基于JQuery、WebApi的ASP.NET MVC插件的代码生成项目主要技术解析...

    YbSoftwareFactory目前已可快速生成ASP.NET  WebForm.MVC.WinForm和WPF的解决方案源代码,所生成的源代码可直接在VS中打开并运行.终端用户还可自行二次开发自己 ...

  3. 异步编程 In .NET(转载)

    概述 在之前写的一篇关于async和await的前世今生的文章之后,大家似乎在async和await提高网站处理能力方面还有一些疑问,博客园本身也做了不少的尝试.今天我们再来回答一下这个问题,同时我们 ...

  4. .NET 6 平台系列1 .NET Framework发展历程

    自1995年互联网战略日以来最雄心勃勃的事业 -- 微软.NET战略, 2000年6月30日. 微软公司于2002年2月13日正式推出第一代.NET平台 .NET Framewrok 1.0.借助于自 ...

  5. WinForm中的MVC模式--MVP模式

    本文主要介绍MVC模式在WINFORM中的实现,其实砖家们都称它为MVP模式,小弟E文不太好,真的是记不住那个P怎么拼写的.. MVC模式主要解决的问题就是将表示层和业务层进行分离,在以往做WINFO ...

  6. My.WinformMvc,一个 Winform MVC 框架

    缘起 一般在做 WINFORM 开发时,通常都是将很多业务逻辑直接写在 Form 的事件代码里,造成业务逻辑就和界面逻辑紧密耦合在一起.这种情况下,如果业务逻辑稍有变化,涉及的改动量非常大,甚至于整个 ...

  7. C++设计模式——从多态到观察者模式到MVC架构

    我们先从宏观整体上把握MVC的架构与流程:

  8. MVC 之 架构的基本原理及Asp.Net实现MVC

    一.引言 许多Web应用都是从数据存储中检索数据并将其显示给用户.在用户更改数据之后,系统再将更新内容存储到数据存储中.因为关键的信息流发生在数据存储和用户界面之间,所以很多应用将数据和用户界面这两部 ...

  9. GUI应用程序架构的十年变迁:MVC,MVP,MVVM,Unidirectional,Clean

    十年前,Martin Fowler撰写了GUI Architectures一文,至今被奉为经典.本文所谈的所谓架构二字,核心即是对于富客户端的代码组织/职责划分.纵览这十年内的架构模式变迁,大概可以分 ...

最新文章

  1. 谈“云”色变?近80%企业曾遭受数据泄露
  2. AI大事件 | OpenAI员工离职创立机器人新公司,spaCy v2.0.0发布
  3. python中requests库的用途-python中requests.session的妙用
  4. boost::range_result_iterator相关的测试程序
  5. iOS常见问题(5)
  6. VelocityTracker简要
  7. OpenTSDB 造成 Hbase 整点压力过大问题的排查和解决
  8. 根据函数名称调用函数
  9. IE浏览器无法打开的解决方案
  10. 如何能写出,一份让 HR 认同的简历
  11. pytho---之easydict使用
  12. 5. Keras - CNN应用于手写数字识别
  13. 初识STM32与其选型
  14. PLECS的热模型无法导入的解决办法
  15. python--实现汇率转换
  16. Revisiting Stereo Depth Estimation From a Sequence-to Sequence Perspective with Transformer——阅读阶段
  17. 昆仑ONLINE外挂脚本--基于Seraph
  18. 微信点餐小程序开发_分享微信点餐小程序可以实现哪些功能
  19. 央视新闻同款 教你1分钟做出三维地球动画特效
  20. [BZOJ4430][Nwerc2015]Guessing Camels赌骆驼

热门文章

  1. 【源码】牛顿-拉斐逊法求解非线性方程组
  2. 电大计算机网络形成性考核册,2017电大《计算机应用基础(Win7)》形成性考核册(参考答案)...
  3. 业务流程图 VS 功能流程图 VS 页面流程图
  4. js中输出html代码怎么写,JavaScript输出语句
  5. 无线通信网络学习之E-UTRAN与EPC篇(20141209)
  6. intel 插桩工具 pin 介绍
  7. linux安装配置软件厂库,Centos8 安装 Gogs 代码仓库管理工具
  8. C Primer Plus 第六版 章节课后编程练习答案(下)(缘更)
  9. Linux查看tomcat服务进程号,Linux下启动停止查看杀死Tomcat进程
  10. 慕尼黑工业大学计算机博士申请条件,德国慕尼黑工业大学博士留学有哪些申请条件...