package class7_3_RemoteControl_inClass_start.other; import java.util.LinkedList; /** * This is a "helper" class that handles the details of invocation of specific commands * @author hornick * */ public class Invoker { private LinkedList stackOfExecutedCommands = new LinkedList<>(); private LinkedList stackOfUnexecutedCommands = new LinkedList<>(); /** * constructor */ public Invoker() { } /** * tells this to execute its Command immediately */ public boolean invoke( Command cmd ) { boolean success = cmd.execute(); stackOfExecutedCommands.push(cmd); stackOfUnexecutedCommands.clear(); return success; } /** * tells this to undo the last Command (if possible) */ public boolean undo() { Command lastCommand = stackOfExecutedCommands.pop(); boolean success = lastCommand.unexecute(); if(success) { stackOfUnexecutedCommands.push(lastCommand); } return success; } public boolean redo() { Command nextCommand = stackOfUnexecutedCommands.pop(); boolean success = nextCommand.execute(); if(success) { stackOfExecutedCommands.push(nextCommand); } return success; } }