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

 
刚一个开始看PureMVC,一个字乱,摸不着头脑,不过这个要慢慢的啃,消化,看PureMVC文档要结合它的类图看.
在这里给大家分享一下PureMVC中的观察者模式运用.
观察模式中有主题 ,观察者,通知.在PureMVC中 View充当主题的角色, Observer 当然充当观察者了,Notification 就充当命令通知了.
现在大家都知道PureMVC中观察者模式的角色分别是谁了,这样在去看就非常清晰了,但它的来胧去脉是怎么回事那?
在View中定义了一些方法,主要的方法:
Java代码
  1. +registerObserver(in notificationName : String, in observer : IObserver) : void  
  2. +notifyObservers(in notification : INotification) : void  
  3. +registerMediator(in mediator : IMediator) : void  
  4. +retrieveMediator(in mediatorName : String) : IMediator  
  5. +removeMediator(in mediatorName : String) : void  
  6. +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方法对其进行调用:
Java代码
  1. public function executeCommand( note : INotification ) : void  
  2. {  
  3.   var commandClassRef : Class = commandMap[ note.getName() ];  
  4.   if ( commandClassRef == null ) return;  
  5.      var commandInstance : ICommand = new commandClassRef();  
  6.     commandInstance.execute( note );  
  7.   }  
  8.   
  9. public function registerCommand( notificationName : String, commandClassRef : Class ) : void  
  10. {  
  11.   if ( commandMap[ notificationName ] == null )   
  12.   {  
  13.     view.registerObserver( notificationName, new Observer( executeCommand, this ) );  
  14.     }  
  15.     commandMap[ notificationName ] = commandClassRef;  
  16.    }  
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在构造方法,构造两个参数
Java代码
  1. public function Observer( notifyMethod:Function, notifyContext:Object )   
  2. {  
  3.   setNotifyMethod( notifyMethod );  
  4.   setNotifyContext( notifyContext );  
  5. }  
	public function Observer( notifyMethod:Function, notifyContext:Object ) 
	{
	  setNotifyMethod( notifyMethod );
	  setNotifyContext( notifyContext );
	}

notifyMethod 这个参数是其实是executeCommand方法。
Observer接收到通知后,执行方法:
Java代码
  1. public function notifyObserver( notification:INotification ):void  
  2. {  
  3.  this.getNotifyMethod().apply(this.getNotifyContext(),[notification]);  
  4. }  
public function notifyObserver( notification:INotification ):void
{
 this.getNotifyMethod().apply(this.getNotifyContext(),[notification]);
}




发表时间:2008-08-07
注册后就存在一个Map中
是不是必须手工清理掉注册的对象?
1 积分不足30无法投票
发表时间:2008-08-11
里面的类都是一些单例,所以里面只会存在一个注册的对象.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值