................................................................13 PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. PureMVC PureMVC PureMVC是一个轻量级框架,用于创建基于经典的模型-视图-控制器元模式的程序。
该免费的
开源框架可以应用于包括ActionScript 2、 ActionScript 3、Java、C和其他一些流行的编程语言。
他可以被部署在各种平台下,包括: ?? 移动环境:FlashLite .NET Compact Framework J2ME ?? 服务器环境:ColdFusion J2EE
PHP Python ?? 浏览器环境:Flash/Flex
JavaFX Silverlight ?? 桌面环境.
NET AIR FLASH J2SE PureMVC有一个非常狭隘的主要目标:用于帮助程序员分离
代码为三个离散的层:即模型,视图和控制器。
完成这个目标,推出一个稳定的框架是本文的主要目的。
而通过提供一些实例和工具来阐述本框架的作用是接下来的
工作。
其它一些最初是由ActionScript3文献推动的目标有: ?? 简单且实用的框架范围 ?? 减少由应用层、类的作用、职责和组合造成的程序混乱 ?? 平衡可维护性与可扩展性之间的关系 ?? 提供清晰和完备的文献 ?? 避免平台依赖性 ?? 对开发者隐藏复杂性 ?? 优秀的框架不需要持续的维护以推出新的特性 框架的选择会严重影响到应用架构,进而影响到成本维护等。
所以需要慎重选择。
使用PureMVC框架进行开发具备下列好处: 1 基于设计模式 ?? 支持已经被时间所证明的
设计模式,可以部署于任何具备支持面向对象的环境 ?? 简单易懂 2 实用的实现接口的类 ?? 框架是“即开即用”的,只需要很小的实现需求 PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. ?? Fa??ade类提供了与核心类交互的框架 3 松耦合架构 ?? 提供可复用的组件、模型数据对象和服务 ?? 包含发布/订阅风格(注:观察者模式)的通知 ?? 建立明确的分离客户端三层编码关系 4 面向接口的编程 ?? 框架支持子类或实现接口的扩展 ?? 对所有的框架类定义接口 ?? 所有的类基于可扩展性思想,所有protected修饰的方法、属性、接口参数基于方法签名 ?? 框架支持将来通过其他类库以分组模式进行扩展,并且与核心包的模式互不依赖 5 完备的
文档 ?? 源
代码免费开放 ?? 完备的源
代码文档 ?? 概念和统一建模语言(UML)视图 ?? 所有类和接口的单元测试 ?? 大量的示例
代码和入门教程 ?? 由架构师撰写的《术语阐述与最佳实践》 ?? 已有数千名学生测试专业
课程 UML 本文讨论PureMVC框架的类和接口,通过简单的UML(统一建模语言)图表阐述他们的作用、职责和合作。
PureMVC有一个非常狭隘的主要目标:用于帮助程序员分离
代码为三个离散的层:即模型,视图和控制器。
在该经典的MVC元模式实现框架中,应用层有三个单例(一个只创建一次的类)所表示。
而第四个单例,Fa??ade类通过一个单一的接口为应用程序之间提供通讯,从而简化了开发。
?? 模型(Model)简单的指向Proxy的引用。
Proxy
代码负责操作数据模型,与远程服务通信存取数据。
?? 视图指向Mediator的引用。
Mediator管理视图组件,增加事件监听器,操作视图组件的状态。
?? 控制器包括命令类及其映射。
命令类是无状态的,只有需要时才被创建。
?? Fa??ade单例初始化Model View Controller,并且能访问各层类的Public方法。
PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. Fa??ade Fa??ade类令roxies Mediators和Commands之间以松耦合的方式允许互相
通信,而不需要导入或知道框架核心的存在。
当我们创建一个Fa??ade类具体的应用时,就可以“即开即用”的使用它了。
附带的,与Fa??ade类的交互将会最大化的降低开发者对API知识的了解程度。
核心成员类Model, View和Controller分别实现了IModel IView 以及 IController 接口,Fa??ade类实现了由核心接口组成的IFacade接口。
View Mediators and View View类被实现为一个用来缓存指向一个IMediator实例引用的单例。
Mediators类帮助我们创建或者重用已经存在的用户接口组件,而不用让他们通讯的PureMVC程序的知识去影响他们。
具体的Mediators实例必须实现IMediator接口,通常是作为Mediator类的子类来实现的。
View视图用来显示数据和处理用户交互。
在一个基于flash的应用
程序中,他们通常通过使用事件和传递一些属性给其Mediators类去检查或者控制,以便与View的Mediators类 PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. 交互。
一个Mediator类通过View组件的数据与之建立联系。
当一个Mediator 类的实例被view注册时,他会被假设为他需要获取通知。
因此必须返回一个包含所有他感兴趣数据的通知名数组。
因为Mediator类必须实现IMediator接口,所以一个Mediator实例拥有handleNotification方法。
当该实例被View组件注册,一个Observer实例就被创建并且被注册给每一个Notification的数组中。
这样Mediator的handleNotification方法就被触发,而不论注册Notification的Mediator是否感兴趣。
Mediator类由于实现了INotifier接口而具备了sendNotification方法,sendNotification方法的参数是一个新的Notification通知,而后使用一个IFacade接口的单例去广播。
PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. Mediator类的protected属性被初始化用于
注册IFa
cade实例,并且因此Mediator必须在主程序初始化Fa??ade实例后被注册。
PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s
websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. Model Proxies Model类被实现为一个简单的指向IProxy引用的单例。
Proxy类帮助我们暴露数据模型和实体类(当域逻辑和服务支持时)给应用程序,使得程序可以被其它地方被复用或重构。
我们可能通过使用一个Proxy实例来简化指向本地数据对象的管理,在这种情况下交互可能引起一些数据的同步操作。
一个Proxy实例也可能被用来进行程序的
原创服务操作,加载或者保持数据。
这时,我们可能需要出发一个方法或者基于Proxy去设置数据,并且等待该Proxy从服务器成功加载到数据后广播一个Notification通知。
Proxy类由于实现了INotifier接口而具备了sendNotification方法,sendNotification方法的参数是一个新的Notification通知,而后使用一个IFacade接口的单例去广播。
Proxy类的protected属性被初始化用于注册IFacade实例,并且因此Mediator必须在主 PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. 程序初始化Fa??ade实例后被注册。
ControllerCommands MacroCommand类从它的构造函数中触发initializeMacroCommand方法。
使用者必须在其子类中重写initializeMacroCommand方法,一般addSubCommand方法添加所有Command。
可以使用SimpleCommands 或者 MacroCommands中的任何一种。
PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. View ObserverNotification Proxies,Mediators与Commands类之间的通讯是通过广播Notifications来实现松耦合和跨平台特性的: ?? Proxies可以发送,但不能接收Notification ?? Mediator声明感兴趣的Notification和接收Notification ?? Commands被触发或广播Notifications PureMVC应用程序可能运行在不能访问Flash Event和EventDispatcher类的环境中,所以框架使用观察者模式以一种松耦合的方式在Model View Controller之间通信。
PureMVC使用观察者模式就是为了该目的。
IObserver携带一个希望被关注(即“被关 PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. 注的内容”)的对象引用,该对象的一个方法将会在INotification实例被广播(即“被关注方法”)时被触发。
View组件的职责是管理Notification的名称给Observer
列表,以及当有Notification被发送时通知所有的Observers。
PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. IFacade IModel IView PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. IController IProxy IMediator ICommand INotifier PureMVC is a free open source framework created and maintained by Futurescale Inc. Copyright ?? 2006-08 Some rights reserved. Reuse is governed by the Creative Commons 3.0 Attribution Unported License. PureMVC as well as this documentation and any training materials or demonstration source code downloaded from Futurescale’s websites is provided as is without warranty of any kind either express or implied including but not limited to the implied warranties of fitness for a purpose or the warranty of non-infringement. IObserver INotification