分析PureMVC中的观察者模式运用

本文解析了PureMVC框架中的观察者模式应用。详细介绍了主题、观察者及通知等核心概念,并通过具体方法解释了如何实现消息传递与响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚一个开始看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这个方法是注册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;
}

registerMediator这个方法是注册视图,以便视图接收通知.
在Mediator中,既可以发送通知,也可以接收通知.
Observer在构造方法,构造两个参数

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]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值