您的位置:首页 > 其它

设计模式学习-MVC实践

2008-06-11 15:59 337 查看
优点

mvc 模式在项目开发中的优点是能够隔离业务和展示层,进一步的我们有完整的独立对象去控制展示层

直接带来的好处是,我们可以很容易的维护和复用他们。下面你将看到:

一般来讲我们会尽量把项目中对象间的依赖降到最低,这样我们能够更容易的改变和复用他们。为了实现

这样的愿望,我们要遵循“面向接口编程,而不是类”,MVC 就提供了实现它的手段。

我们被分配了一个任务去创建ACME 2000 运动车 业务对象,我们的任务是创建一个简单的Windows 接口

目标:1 显示汽车的当前方向和速度。2 终端用户能够变换汽车方向,加速,减速。当然是在一定允许范围

内。

如果我们设计成功了,我们可能还要设计类似的汽车,敞篷小卡,三轮车,作为开发者 我们也知道 ACME

管理团队最终会说“嗨!这真的很酷”,我们能在公司的内网上看到么?“ 所有这些都想到了,我们就要发布

一个容易测量的产品。

嗯,正好,这就是一个应用MVC 的很好的例子。

开始动手了,

我们要有一个草图,MVC 有三部分:模型 ,控制器和视图。在我们的系统中,模型就是我们的汽车,视图

就是我们的用户接口,控制器就是联结模型和视图的部份。

public enum AbsoluteDirection

public enum RelativeDirection

public interface IVehicleControl

现在我们要创建模型的接口,我们需要知道汽车的名称,速度,最大速度,最大保留速度,最大转弯速度和方向

我们也需要方法去加速,减速,转弯。

public interface IVehicleModel

最后,我们要建立视图接口,我们知道视图应该暴露一些方法给控制器,例如开启和关闭 加速,减速和转弯的请求。

public class IVehicleView

public interface IVehicleControl

public interface IVehicleModel

public class IVehicleView

public abstract class Automobile : IVehicleModel

现在,我们的 "ACME 框架" 已经准备好了,我们必须创建具体的类和我们的接口,然我们先注意最后两个类,会是

我们的控制器和我们的模型。

下面就是我们的具体的汽车控制器,它实现了IVehicleControl 接口,它也会依赖模型来设置视图。(检查setView方法,

它每一次被调用时,都有一个请求被传递到模型)

注意,我们只有到 IVehicleModel 的引用(不是 汽车的抽象类) 。

public class AutomobileControl : IVehicleControl

下面是我们的 ACME2000 运动车类(扩展了汽车抽象类,抽象类实现了 IVehicleModel 接口)

public class ACME2000SportsCar : Automobile

现在来看我们的视图

我们要创建一个AutoView视图用户控件来实现IVehicleView 接口,该视图拥有一个到控制器和模型接口的引用。

public class AutoView : System.Windows.Forms.UserControl, IVehicleView

public AutoView()

public void WireUp(IVehicleControl paramControl, IVehicleModel paramModel)

private void btnAccelerate_Click(object sender, System.EventArgs e)

private void btnDecelerate_Click(object sender, System.EventArgs e)

private void btnLeft_Click(object sender, System.EventArgs e)

private void btnRight_Click(object sender, System.EventArgs e)

//Add a method to update the interface

public void UpdateInterface(IVehicleModel auto)

//Finally, we'll wire up the IVehicleView interface methods

public void DisableAcceleration()

public void EnableAcceleration()

public void DisableDeceleration()

public void EnableDeceleration()

public void DisableTurning()

public void EnableTurning()

public void Update(IVehicleModel paramModel)

public class ACME2000Truck: Automobile

//in the AutoView, we just have to build the truck and wire it up!

private void btnBuildNew_Click(object sender, System.EventArgs e)

//If we wanted a new Control that only allowed us to increase or decrease the speed by a maximum of 5mph, it's a snap! Create a SlowPokeControl (same as our AutoControl, but with limits on how much a Model will be requested to accelerate)

public void RequestAccelerate(int paramAmount)

public void RequestDecelerate(int paramAmount)

//If we want to make our ACME2000 Truck a SlowPoke, we just wire it up in the AutoView!

private void btnBuildNew_Click(object sender, System.EventArgs e)

结论:

你可以看到,使用MVC帮助创建代码去控制接口,这使得当我们改变请求时更容易。

你可以复用你的接口和抽象类在任何其它地方。一定要在你的下一个项目时想起MVC

你不会后悔的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: