package class8_Lab_RemoteControl_Invoker.other; import java.util.Deque; import java.util.LinkedList; /** * This is a "helper" class that handles the details of invocation of specific commands * @author hornick * */ public class Invoker { private Deque stack = new LinkedList(); /** * constructor */ public Invoker() { } /** * tells this to execute its Command immediately */ public boolean invoke( Command command ) { if(null == command) { System.out.println("Errro!!!!"); } boolean status; status = command.execute(); if(status) { stack.push(command); } return status; } // /** // * tells this to undo the last Command (if possible) // */ // public boolean undo() { // return false; // } /** * This method creates a command to undo things. * This command actually manipulates the internal * stacks of this class as needed to perform * the operation. * @return the undo command. */ public Command getUndoCommand() { return new Command() { @Override public boolean execute() { boolean status; Command lastCommand = stack.pollFirst(); if(null != lastCommand) { status = lastCommand.unexecute(); } else { status = false; } return false; // dilemma: If we return true, (e.g. status) // THIS gets pushed onto the UNDO stack // If we return "false", the command "fails" } @Override public boolean unexecute() { return false; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getCommandName() { return "Undo"; } }; } }