Example: Implementing methods from the TargetableCommand interface
The TargetableCommand interface declares one method, performExecute, that application programmer must implement.
Methods from the TargetableCommand interface in the ModifyCheckingAccountCmdImpl class
The following code example shows the implementations for the ModifyCheckingAccountCmd command. The implementation of the performExecute method is as follows:
- Saves the current balance (so the command can be undone by a compensator command)
- Calculates the new balance
- Sets the current balance to the new balance
- Ensures that the hasOutputProperties method returns true so that the values are returned to the client
In addition, the ModifyCheckingAccountCmdImpl class overrides the default implementation of the setOutputProperties method.
... public class ModifyCheckingAccountCmdImpl extends TargetableCommandImpl implements ModifyCheckingAccountCmd { ... // Method from the TargetableCommand interface public void performExecute() throws Exception { CheckingAccount checkingAccount = getCheckingAccount(); oldBalance = checkingAccount.getBalance(); balance = oldBalance+amount; checkingAccount.setBalance(balance); setHasOutputProperties(true); } public void setOutputProperties(TargetableCommand fromCommand) { try { if (fromCommand != null) { ModifyCheckingAccountCmd modifyCheckingAccountCmd = (ModifyCheckingAccountCmd) fromCommand; this.oldBalance = modifyCheckingAccountCmd.getOldBalance(); this.balance = modifyCheckingAccountCmd.getBalance(); this.checkingAccount = modifyCheckingAccountCmd.getCheckingAccount(); this.amount = modifyCheckingAccountCmd.getAmount(); } } catch (Exception ex) { System.out.println("Error in setOutputProperties."); } } ... }
Related tasks
Implementing command interfaces
Reference topic