Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Command Registration questions  (Read 12221 times)
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« on: December 13, 2007, 10:04:20 »

Thomas asked:
- 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:

:
        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:

:
        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 by puremvc » Logged
arasoft
Port to Java
Newbie
*
Posts: 2


View Profile Email
« Reply #1 on: December 14, 2007, 04:25:32 »

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: 2871



View Profile WWW Email
« Reply #2 on: December 14, 2007, 01:20:09 »

Yes, since the observer will point to the same place, the controller's executeCommand method. I agree.
-=Cliff>
Logged
Pages: [1]
Print