Compensating command example
To use a compensating command, retrieve the compensator that is associated with the primary command and call its execute method.
ModifyCheckingAccountCompensator command
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()); } ... }
Related tasks
Use a command
Reference topic