`
steely816
  • 浏览: 126697 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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]);   


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 方法。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics