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>