PureMVC Architects Lounge

Announcements and General Discussion => Architecture => Topic started by: Durairaj on May 16, 2009, 04:10:13



Title: Clarification in View
Post by: Durairaj on May 16, 2009, 04:10:13
Hi Cliff,

I was digging through in the framework classes and i found something which is not needed or may be i didnt get it correctly? Could you please explain the same

In view class ,

public function registerObserver ( notificationName:String, observer:IObserver ) : void
      {
         var observers:Array = observerMap[ notificationName ];
         
         if( observers ) {
            observers.push( observer );
         } else {
            observerMap[ notificationName ] = [ observer ];   
         }
      }


There is no necessity for pushing the observer into observers local scope array. Since it doesnt do any other function. Is this done for code clarity or i got it wrong?

Durai


Title: Re: Clarification in View
Post by: puremvc on May 17, 2009, 08:33:11
It is mostly for clarity, yes. The observers variable in this method is merely a reference to the the array that may or may not exist in observerMap[ notificationName ], it isn't a copy of that array. It is defined so that we may easily evaluate whether the new observer needs be pushed onto the existing array, or have one created if this is the first observer for the notification.

It could be written as below, but that would be less clear and I doubt any more efficient:

:
public function registerObserver ( notificationName:String, observer:IObserver ) : void
{         
  if( observerMap[ notificationName ] ) {
      Array(observerMap[ notificationName ]).push( observer );
  } else {
      observerMap[ notificationName ] = [ observer ];   
  }
}

-=Cliff>