To use a compensating command, you must retrieve the compensator that is associated with the primary command and call its execute method.
The following code example shows the code used to run the original command and to give the user the option of undoing the work by running the compensating command.
{ ... CheckingAccount checkingAccount .... try { ModifyCheckingAccountCmd cmd = new ModifyCheckingAccountCmdImpl(null, 1000); cmd.setCheckingAccount(checkingAccount); cmd.execute(); ... System.out.println("Would you like to undo this work? Enter Y or N"); try { // Retrieve and validate user's response ... } ... if (answer.equalsIgnoreCase(Y)) { Command compensatingCommand = cmd.getCompensatingCommand(); compensatingCommand.execute(); } } catch (Exception e) { System.out.println(e.getMessage()); } ... }