PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: mani on July 07, 2008, 12:08:50



Title: The confusion about Notfication
Post by: mani on July 07, 2008, 12:08:50
Hello, nameless hero~
I encountered three problem about Notification of PureMVC, please have a look, Thanks for your time~

public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void
      {
         notifyObservers( new Notification( notificationName, body, type ) );
      }
It's a method of Facade, and there will new a Notfication() instance when Facade call sendNotification each time.
Q1:Why don't make Notification to be singleton instance, as this:

//The method of Notification to configure Notification
public function setNotification(name:String, body:Object=null, type:String=null):INotification
      {
         this.name = name;
         this.body = body;
         this.type = type;
         return this;
      }

//some code section of Facade.
private var _notification:Notification = Notification.getInstance();
public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void
      {
         notifyObservers( _notification.setNotification(notificationName, body, type));
      }

public function notifyObservers(note:INotification):void
      {
         if(view != null) view.notifyObservers(note);
      }

then goto the method notifyObservers of View:
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 as IObserver;
               observer.notifyObserver(notification);
            }
         }
      }

and then goto the mothod notifyObserver of Observer:
Q2: what's mean of [this.getNotifyMethod().apply(this.getNotifyContext(),[notification])], I feel a bit  dizzy~
I think it means
public function notifyObserver(notification:INotification):void
      {
         //thisArg:* - The object to which the function is applied.
         //argArray:* - An array whose elements are passed to the function as parameters.
         this.getNotifyMethod().apply(this.getNotifyContext(),[notification]);
      }

this is a common method in ApplicationFacade:
public function startup(app:Object):void
       {
          sendNotification( STARTUP, app );   
       }
Q3:Anybody could explain the Notification's inner chain of responsibility mechanism?
In my opinion:
First, Application call sendNotfication(notificationName:String, body:Object=null, type:String=null);
then, inner view call notifyObservers(note:INotification);
then, check out observerMap(In registerMediator method, according to mediator.listNotificationInterests() to registerObserver,
                                           then add element into observersMap),to see if the notification exists, and call notifyObserver.
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 as IObserver;
               observer.notifyObserver(notification);
            }
         }
then, .... I don't know how to say, the point is the mean of {this.getNotifyMethod().apply(this.getNotifyContext(),[notification])}, how it works, and then what happened?

Best regards~~
mani.


Title: Re: The confusion about Notfication
Post by: creynders on July 07, 2008, 01:30:03
Q1: if you turn it into a singleton, you can only send one notification at a time. Which would be a serious downgrade.

Q2: this.getNotifyMethod().apply(this.getNotifyContext(),[notification]);
means:
apply the result of getNotifyMethod (that result is a function) to the result of the getNotifyContext and pass the retrieved function the value of "notification" (the apply method takes an array of possible arguments, therefore notification is enclosed within [])

Q3:
It provides a way for a class to define what method should be invoked when a notification is received.

For instance in Controller.registerCommand there's the line (#136)
view.registerObserver( notificationName, new Observer( executeCommand, this ) );

Controller passes it's own "executeCommand" method to the Observer constructor ( that method is stored in the "notify" property of the Observer class) So, whenever the getNotifyMethod of that Observer instance is called it will return the "executeCommand" method of Controller.
As a second parameter it passes "this" (which is the instance of the Controller class) which gets stored inside the Observer instance in the "notifyContext" property. So, whenever the getNotifyContext is called it will return the controller instance.

Whenever the notifyObserver method of an Observer instance (used in the Controller class) is called it will run the "executeCommand" method on the controller instance. (This, obviously, only applies to all Observer instances used in the Controller class, Observer instances in other classes will return other methods and other contexts)


Title: Re: The confusion about Notfication
Post by: mani on July 07, 2008, 02:58:09
I got it~
Thank you , creynders :)