org.puremvc.java.patterns.command.MacroCommand Maven / Gradle / Ivy
Show all versions of PureMVC Show documentation
//
// PureMVC Java Standard
//
// Copyright(c) 2019 Saad Shams
// Your reuse is governed by the Creative Commons Attribution 3.0 License
//
package org.puremvc.java.patterns.command;
import org.puremvc.java.interfaces.ICommand;
import org.puremvc.java.interfaces.INotification;
import org.puremvc.java.patterns.observer.Notifier;
import java.util.Vector;
import java.util.function.Supplier;
/**
* A base ICommand
implementation that executes other ICommand
s.
*
* A MacroCommand
maintains an list of
* ICommand
Class references called SubCommands.
*
* When execute
is called, the MacroCommand
* instantiates and calls execute
on each of its SubCommands turn.
* Each SubCommand will be passed a reference to the original
* INotification
that was passed to the MacroCommand
's
* execute
method.
*
* Unlike SimpleCommand
, your subclass
* should not override execute
, but instead, should
* override the initializeMacroCommand
method,
* calling addSubCommand
once for each SubCommand
* to be executed.
*
* @see org.puremvc.java.core.Controller Controller
* @see org.puremvc.java.patterns.observer.Notification Notification
* @see org.puremvc.java.patterns.command.SimpleCommand SimpleCommand
*/
public class MacroCommand extends Notifier implements ICommand {
private Vector> subCommands;
/**
* Constructor.
*
* You should not need to define a constructor,
* instead, override the initializeMacroCommand
* method.
*
* If your subclass does define a constructor, be
* sure to call super()
.
*/
public MacroCommand() {
subCommands = new Vector>();
initializeMacroCommand();
}
/**
* Initialize the MacroCommand
.
*
* In your subclass, override this method to
* initialize the MacroCommand
's SubCommand
* list with ICommand
class references like
* this:
*
*
* {@code
* // Initialize MyMacroCommand
* protected void initializeMacroCommand( )
* {
* addSubCommand( () -> new com.me.myapp.controller.FirstCommand() );
* addSubCommand( () -> new com.me.myapp.controller.SecondCommand() );
* addSubCommand( () -> new com.me.myapp.controller.ThirdCommand() );
* }
* }
*
*
* Note that SubCommands may be any ICommand
implementor,
* MacroCommand
s or SimpleCommands
are both acceptable.
*/
protected void initializeMacroCommand() {
}
/**
* Add a SubCommand.
*
* The SubCommands will be called in First In/First Out (FIFO)
* order.
*
* @param commandSupplier a reference to the commandSupplier of the ICommand
.
*/
protected void addSubCommand(Supplier commandSupplier) {
subCommands.add(commandSupplier);
}
/**
* Execute this MacroCommand
's SubCommands.
*
* The SubCommands will be called in First In/First Out (FIFO)
* order.
*
* @param notification the INotification
object to be passsed to each SubCommand.
*/
public void execute(INotification notification) {
for(Supplier commandSupplier : subCommands) {
ICommand command = commandSupplier.get();
command.execute(notification);
}
}
}