PureMVC 观察者模式

 

 


 

1)  创建ActionFacade的实例_facadeActionFacadeFaç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çadesendNotification方法

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

}

也就是说要调用ViewnotifyObservers方法

4)  ViewnotifyObservers方法如下,遍历所有关注这个通知(名称)的Observer,依次执行这些ObservernotifyObserver方法。

    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)  下面来看ObservernotifyObserver方法

public function notifyObserver( notification:INotification ):void

              {

                     this.getNotifyMethod().apply(this.getNotifyContext(),[notification]);

              }

这个方法很简单,获取这个Observer封装的响应通知的方法(如exectue()),并把对应的上下文(如Controller),和通知作为参数来执行,呵呵!很像java中的反射机制。

6)  如在观察者模式的应用中讲到的,执行了ControllerexecuteCommand方法,从而遍历Command数组,找到响应这个通知的Command(如LoginCommand),并执行这个Commandexectue方法。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值