Nimbus – Macro Commands
04.29.2009In my recent intro to Nimbus, I mentioned that one of the features of Nimbus is macro commands. In complex applications, macro commands can be a huge time saver and allow you to have a lot of control over handling a group of service calls with minimal code.
Two types of macro commands exist. The first is a SequenceCommand. This means its subcommands will be executed sequentially. In other words, the second subcommand won’t begin executing until the first subcommand has finished executing. The SequenceCommand won’t be considered complete until the last subcommand has finished executing.
The other type of macro command is a ParallelCommand. As you may have guessed, this means its subcommands will be executed in parallel. In other words, the first command and second command will execute at the same time. The ParallelCommand won’t be considered complete until all subcommands have finished executing.
Just as the synergy of the Planeteers can work miracles, so can the synergy of the SequenceCommand and ParallelCommand. Nesting macro commands can give some enhanced control as we’ll see here:
package controller { import nimbus.commands.ParallelCommand; import nimbus.commands.SequenceCommand; public class MyMacroCommand extends SequenceCommand { public function MyMacroCommand() { this.subcommands = [ new CommandA(), new ParallelCommand( [ new CommandB(), new CommandC() ]), new CommandD(), new ParallelCommand( [ new CommandE(), new SequenceCommand( [ new CommandF(), new CommandG() ]), new CommandH(), new CommandI() ]), new CommandJ() ]; } } }
It’s probably easier to see what’s going on just by looking at the code, but here it is in narrative form:
CommandA executes. CommandA completes. CommandB and CommandC execute. Both of them complete in whatever order. CommandD executes. CommandD completes. CommandE, CommandH, and CommandI execute. While they’re executing, CommandF executes, CommandF completes, CommandG executes, and CommandG completes. After CommandE, CommandH, CommandI, CommandF, and commandG all complete, CommandJ executes and then completes.
Note that these subcommands could also be macro commands of their own. For example, CommandH could be extending ParallelCommand and executing several subcommands within itself. Macro commands can be a very powerful tool when managing the order and timing of your application’s processes. Bon appetit!
Tags: complex commands, macro commands, mvc, nimbus, parallel command, sequence command, service calls

