package class8_1_Lab_Command_inClass_start; import java.util.Stack; /** * This is a "helper" class that handles the details of invocation of specific commands * * @author hornick */ public class Invoker { private Stack doneStack = new Stack<>(); private Stack undoneStack = new Stack<>(); /** * tells this to execute its Command immediately */ public void invoke(Command cmd) { cmd.execute(); doneStack.push(cmd); undoneStack.clear(); } /** * tells this to undo the last Command (if possible) */ public void undo() { if (!doneStack.isEmpty()) { Command cmd = doneStack.pop(); cmd.unexecute(); undoneStack.push(cmd); } } /** * */ public void redo() { if (!undoneStack.isEmpty()) { Command cmd = undoneStack.pop(); cmd.execute(); doneStack.push(cmd); } } }