Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
April 26, 2017, 04:23:23 PM
Home Help Search Login Register
News: ATTENTION: Spambots must die! Humans must visit http://contact.futurescale.com to request forum access.
Pages: [1]
Print
Author Topic: Command Registration questions  (Read 7509 times)
puremvc
Global Moderator
Hero Member
*****
Posts: 2865



View Profile WWW
« 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>
« Last Edit: December 13, 2007, 10:06:14 AM by puremvc » Logged
arasoft
Port to Java
Newbie
*
Posts: 2


View Profile
« Reply #1 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?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2865



View Profile WWW
« Reply #2 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>
Logged
Pages: [1]
Print
Jump to: