package example8_1_AfterClass; 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 ) { boolean status; if(command instanceof UndoCommand) { status = undo(); } else { // new Invoker().invoke(command); status = command.execute(); if(status) { stack.push(command); } } return status; } /** * tells this to undo the last Command (if possible) */ public boolean undo() { boolean status; Command lastCommand = stack.pollFirst(); if(null != lastCommand) { status = lastCommand.unexecute(); } else { status = false; } return status; } }