PureMVC Architects Lounge

PureMVC Manifold => Port Authority => Topic started by: puremvc on December 13, 2007, 10:04:20 AM



Title: Command Registration questions
Post by: puremvc on December 13, 2007, 10:04:20 AM
Thomas asked:
Quote
- What should happen when the same command is registered for the same notification string multiple times?
- What should happen when a new command is registered for an already used notifcation string?

When we do something like this:

        Facade f = Facade.getInstance();

        Facade.getInstance().registerCommand("init", InitCommand.class);
        Facade.getInstance().registerCommand("init", InitCommand.class);
        Facade.getInstance().registerCommand("init", InitCommand.class);
       
        Facade.getInstance().registerCommand("init", DataCommand.class);
       
        INotification n = new Notification("init", "whatever", null);
        f.notifyObservers(n);

the execute method of DataCommand (and not InitCommand) is called four(!) times. Is this the behavior you want?


Currently, in the AS3 version, when the same Command is registered for the same notification string multiple times, in the Controller we store only one reference to the registration, the previous commandMap key is overwritten. However, we do register a new observer regardless of whether there was already a Command registered by that name:

Code:
        public function registerCommand( notificationName : String, commandClassRef : Class ) : void
        {
            commandMap[ notificationName ] = commandClassRef;
            view.registerObserver( notificationName, new Observer( executeCommand, this ) );
        }

This was based on the assumption that registration of commands will happen in one place and its easy to see if you've duplicated yourself. It hasn't been a problem, but it should be handled more like this:

Code:
        public function registerCommand( notificationName : String, commandClassRef : Class ) : void
        {
            if ( commandMap[ notificationName ] != null ) return;
            commandMap[ notificationName ] = commandClassRef;
            view.registerObserver( notificationName, new Observer( executeCommand, this ) );
        }

Once a key is registered for a given notification, subsequent attempts to register a Command to that notification should be disregarded.

-=Cliff>


Title: Re: Command Registration questions
Post by: arasoft on December 14, 2007, 04:25:32 AM
Since the documentation states that the command registered last should be the one used, we propose a different implementation which in our Java port currently looks like this:

   public void registerCommand( String notificationName, Class commandClassRef )
   {
      if (null != this.commandMap.put( notificationName, commandClassRef )) return;
      this.view.registerObserver( notificationName, new Observer( new IFunction()
      {
         public void onNotification( INotification notification )
         {
            executeCommand( notification );
         }
      }, this ) );
   }


We replace the command if one was registered before, but do not add another observer. Okay with you?


Title: Re: Command Registration questions
Post by: puremvc on December 14, 2007, 01:20:09 PM
Yes, since the observer will point to the same place, the controller's executeCommand method. I agree.
-=Cliff>