刚一个开始看PureMVC,一个字乱,摸不着头脑,不过这个要慢慢的啃,消化,看PureMVC文档要结合它的类图看.
在这里给大家分享一下PureMVC中的观察者模式运用.
观察模式中有主题 ,观察者,通知.在PureMVC中 View充当主题的角色, Observer 当然充当观察者了,Notification 就充当命令通知了.
现在大家都知道PureMVC中观察者模式的角色分别是谁了,这样在去看就非常清晰了,但它的来胧去脉是怎么回事那?
在View中定义了一些方法,主要的方法:
- +registerObserver(in notificationName : String, in observer : IObserver) : void
- +notifyObservers(in notification : INotification) : void
- +registerMediator(in mediator : IMediator) : void
- +retrieveMediator(in mediatorName : String) : IMediator
- +removeMediator(in mediatorName : String) : void
- +hasMediator(in mediatorName : String) : Boolean
+registerObserver(in notificationName : String, in observer : IObserver) : void +notifyObservers(in notification : INotification) : void +registerMediator(in mediator : IMediator) : void +retrieveMediator(in mediatorName : String) : IMediator +removeMediator(in mediatorName : String) : void +hasMediator(in mediatorName : String) : Boolean
这个方法中有两个注册方法,registerObserver这个方法是注册Command的执行方法.在Control中有个registerCommand方法对其进行调用:
- public function executeCommand( note : INotification ) : void
- {
- var commandClassRef : Class = commandMap[ note.getName() ];
- if ( commandClassRef == null ) return ;
- var commandInstance : ICommand = new commandClassRef();
- commandInstance.execute( note );
- }
- public function registerCommand( notificationName : String, commandClassRef : Class ) : void
- {
- if ( commandMap[ notificationName ] == null )
- {
- view.registerObserver( notificationName, new Observer( executeCommand, this ) );
- }
- commandMap[ notificationName ] = commandClassRef;
- }
public function executeCommand( note : INotification ) : void { var commandClassRef : Class = commandMap[ note.getName() ]; if ( commandClassRef == null ) return; var commandInstance : ICommand = new commandClassRef(); commandInstance.execute( note ); } public function registerCommand( notificationName : String, commandClassRef : Class ) : void { if ( commandMap[ notificationName ] == null ) { view.registerObserver( notificationName, new Observer( executeCommand, this ) ); } commandMap[ notificationName ] = commandClassRef; }
registerMediator这个方法是注册视图,以便视图接收通知.
在Mediator中,既可以发送通知,也可以接收通知.
Observer在构造方法,构造两个参数
- public function Observer( notifyMethod:Function, notifyContext:Object )
- {
- setNotifyMethod( notifyMethod );
- setNotifyContext( notifyContext );
- }
public function Observer( notifyMethod:Function, notifyContext:Object ) { setNotifyMethod( notifyMethod ); setNotifyContext( notifyContext ); }
notifyMethod 这个参数是其实是executeCommand方法。
Observer接收到通知后,执行方法:
- public function notifyObserver( notification:INotification ): void
- {
- this .getNotifyMethod().apply( this .getNotifyContext(),[notification]);
-
}
1) 创建 ActionFacade 的实例 _facade , ActionFacade 是 Façade 的实现类,并注册相关的 Command ,如在观察者模式讲到的,把相关的 Command 封装到 Observer 中,并注册到 View 内,其响应的通知名称为“ login ”
override protected function initializeController( ) : void
{
super.initializeController();
registerCommand( STARTUP, StratCommand );
this.registerCommand( LOGIN, LoginCommand );
}
2) 调用 _facade.login(user)
public function login( user:UserVo ):void
{
sendNotification( LOGIN, user );
}
3) 实际上是调用父类 Façade 的 sendNotification 方法
public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void
{
notifyObservers( new Notification( notificationName, body, type ) );
}
而 Façade 方法 notifyObservers
public function notifyObservers ( notification:INotification ):void {
if ( view != null ) view.notifyObservers( notification );
}
也就是说要调用 View 的 notifyObservers 方法
4) View 的 notifyObservers 方法如下,遍历所有关注这个通知(名称)的 Observer ,依次执行这些 Observer 的 notifyObserver 方法。
public function notifyObservers( notification:INotification ) : void
{
if( observerMap[ notification.getName() ] != null ) {
var observers:Array = observerMap[ notification.getName() ] as Array;
for (var i:Number = 0; i < observers.length; i++) {
var observer:IObserver = observers[ i ] as IObserver;
observer.notifyObserver( notification );
}
}
}
5) 下面来看 Observer 的 notifyObserver 方法
public function notifyObserver( notification:INotification ):void
{
this.getNotifyMethod().apply(this.getNotifyContext(),[notification]);
}
这个方法很简单,获取这个 Observer 封装的响应通知的方法(如 exectue ()),并把对应的上下文(如 Controller ),和通知作为参数来执行,呵呵!很像 java 中的反射机制。
6) 如在观察者模式的应用中讲到的,执行了 Controller 的 executeCommand 方法,从而遍历 Command 数组,找到响应这个通知的 Command (如 LoginCommand ),并执行这个 Command 的 exectue 方法。
发表评论
-
AS3 父SWF与子SWF之间传值
2011-02-20 15:19 2583FLASH中 父SWF加载子SWF并向子SWF传值在很多应用上 ... -
flash as3.0 被加载的swf 控制主swf文件触发事件
2011-02-19 14:48 2983比如有两个swf文件 1.swf,2.swf 1.swf ... -
一些很有创意的导航栏设计,给大家分享下。
2011-02-19 11:08 1542希望对各位同学有所帮助。啊哦………… 陆续完善i ... -
浅谈webgame游戏开发技术要点
2011-02-19 11:02 959写这些只是想和大家交 ... -
aswing
2011-02-19 10:43 827AsWing是一套UI组件框架,纯ActionScript开发 ... -
as3加载外部XML文件
2011-02-18 19:50 1410加载过程如下: 1.使用URLRequ ... -
如何保护swf文件不被反编译软件破解?
2011-02-18 19:42 2132How to protect SWFs from decomp ... -
45度地图编辑器及游戏开发心得
2011-02-15 19:12 1384转载 这个编辑器是本人为公司项目专门开发 的一个功能比较齐 ... -
BulkLoader开源文件加载类
2011-02-09 22:10 1186BulkLoader是一个不错且使用简单的开源加载类, ... -
社区客户端开发技术总结
2011-01-29 22:57 823不管是什么样的项目,都会拆分成一个一个小的模块。最终实现都是些 ... -
“神之惩戒”--国内MMORPG游戏的困局1
2011-01-28 15:13 749《神之惩戒——MMOR ... -
adobe alchemy资料整理
2011-01-28 14:56 2408Adobe 自从2007年中推出了AS3支持 ... -
hitTest(BitmapData.hitTest 方法)
2011-01-25 18:14 1138public hitTest(firstPoint:Poi ... -
AS3使用外部SWF文件的库文件
2011-01-25 15:32 1430在swf文件中使用外部swf文件的库文件,从而达到类似素材 ... -
Flash摄像头游戏课件设计与开发
2011-01-25 11:07 1176[导读] Flash摄像头游戏课件设计与开发 ... -
AS3图像复制BitmapData.copyPixels()
2011-01-22 16:35 1905//舞台属性设置 stage.align ... -
ps样式介绍
2011-01-22 14:32 820PS样式就是效果的集合。“窗口”“样式”就可以打开样式面板。里 ... -
获取当前SWF的绝对路径
2011-01-20 19:22 1341首先 ,说明我测试的系统地址和网络地址的情况: 某电脑 ... -
获取当前SWF的当前路径
2011-01-19 20:00 1414首先 ,说明我测试的系统地址和网络地址的情况: 某电脑 ... -
Event.INIT Event.COMPLETE 区别
2011-01-19 11:50 1251package{ import flash ...
相关推荐
运用观察者模式及MVC模式的整合,是javaWeb的简单的实现了选民投票
PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助PureMVC中文帮助
接下来是将puremvc的的库添加到您的Flex的或是空气项目的构建路径中,在FlashBuilder4中的操作步骤是:项目- >属性- > Flex的构建路径- >库路径- >添加深港西部通道,浏览并选择D组:/ download/PureMVC_AS3_2_0_4/...
pureMVC中文手册,对pureMVC框架进行了较详细的介绍,很适合初学者使用。
PureMVC五子棋游戏源码,学习PureMVC
PureMVC框架 PureMVC中文文档 PureMVC教程
PureMVC教程,是学习PureMVC框架的好文章,PureMVC教程,是学习PureMVC框架的好文章,PureMVC教程,是学习PureMVC框架的好文章PureMVC教程,是学习PureMVC框架的好文章,PureMVC教程,是学习PureMVC框架的好文章,PureMVC...
用PureMvc开发Air项目,完整工程实例。 学PureMvc的第一个完整例子
这个是一个根据AS3(ActionScript 3) pureMVC而转换过来的lua pureMVC。所有的接口完全跟AS3版本一致...注意,这个PureMVC中的 class(ClassName, BaseName) 函数并不提供,因为此框架本意就是为了用于cocos2d-x-lua中。
本文以初学者的视角,详细地介绍PureMVC在ASP.NET的应用。 PureMVC不仅仅是Flex的MVC框架哦,而且它非常的小巧,还等什么呢,快来试用一下吧。 PureMVC的设计思想真的是很棒的,如果再加上C#3.0的LINQ,那就更加的...
flex PureMVC框架实例 flex PureMVC框架
pureMVC项目包 导入的包 pureMVC项目包pureMVC项目包
压缩包里面有一个用puremvc制作的as实例,另附上一个puremvc的中文文档供大家参考。
pureMVC框架 pureMVC源码 学习pureMVC的知识
按照puremvc官方网站教程做的登陆实例,在flexBuilder3上运行过.绝对好用. 供大家学习参考.
本例子运用PureMVC框架编写的简单例子,初学者可以通过本例子理解PureMVC的实现过程,各层的主要职责及如何通信.
Pure MVC是在基于模型、视图和控制器MVC模式建立的一个轻量级的应用框架,此资源是PureMVC standard 单线程开发框架.
这是一个MVC例子,简单的接受了MVC的实现过程。
介绍了PureMVC框架结构以及PureMVC下消息发送机制,有助于更好的了解和学习PureMVC架构
PureMVC学习资料与案例PureMVC学习资料与案例PureMVC学习资料与案例PureMVC学习资料与案例