Command-specific methods
The ModifyCheckingAccountCmd interface defines several command-specific methods in addition to extending other interfaces in the command package. These command-specific methods are implemented in the ModifyCheckingAccountCmdImpl class.
Code example: Constructors in the ModifyCheckingAccountCmdImpl class
You must provide a way to instantiate the command. The command package does not specify the mechanism, so you can choose the technique most appropriate for your application. The fastest and most efficient technique is to use constructors. The most flexible technique is to use a factory. Also, since commands are implemented internally as JavaBeans components, you can use the standard Beans.instantiate method. The ModifyCheckingAccountCmd command uses constructors.
... public class ModifyCheckingAccountCmdImpl extends TargetableCommandImpl implements ModifyCheckingAccountCmd { // Variables ... // Constructors // First constructor: relies on the default target policy public ModifyCheckingAccountCmdImpl(CommandTarget target, float newAmount) { amount = newAmount; checkingAccount = (CheckingAccount)target; setCommandTarget(target); } // Second constructor: allows you to specify a custom target policy public ModifyCheckingAccountCmdImpl(CommandTarget target, float newAmount, TargetPolicy targetPolicy) { setTargetPolicy(targetPolicy); amount = newAmount; checkingAccount = (CheckingAccount)target; setCommandTarget(target); } ... }This code example shows the two constructors for the command. The difference between them is that the first uses the default target policy for determining the target of the command and the second allows you to specify a custom policy. For more information on targets and target policies, see “Targets and target policies”.
Both constructors take a CommandTarget object as an argument and cast it to the CheckingAccount type. The CheckingAccount interface extends both the CommandTarget interface and the EJBObject (see Figure 80 on page 160). The resulting checkingAccount object routes the command to the desired server by using the bean’s remote interface. (For more information on CommandTarget objects, see “Writing a command target (server)” on page 159.)
Code example: Command-specific methods in the ModifyCheckingAccountCmdImpl class
... public class ModifyCheckingAccountCmdImpl extends TargetableCommandImpl implements ModifyCheckingAccountCmd { // Variables ... // Constructors ... // Methods in ModifyCheckingAccountCmd interface public float getAmount() { return amount; } public float getBalance() { return balance; } public float getOldBalance() { return oldBalance; } public float setBalance(float amount) { balance = balance + amount; return balance; } public float setBalance(int amount) { balance += amount ; return balance; } public TargetPolicy getCmdTargetPolicy() { return getTargetPolicy(); } public void setCheckingAccount(CheckingAccount newCheckingAccount) { if (checkingAccount == null) { checkingAccount = newCheckingAccount; } else System.out.println("Incorrect Checking Account (" + newCheckingAccount + ") specified"); } public CheckingAccount getCheckingAccount() { return checkingAccount; } ... }This code example shows the implementation of the following command-specific methods:
- setBalance - sets the balance of the account.
- getAmount - returns the amount of a deposit or withdrawal.
- getOldBalance, getBalance - capture the balance before and after an operation.
- getCmdTargetPolicy - retrieves the current target policy.
- setCheckingAccount, getCheckingAccount - set and retrieve the current checking account.
The ModifyCheckingAccountCmd command operates on a checking account. Because commands are implemented as JavaBeans components, you manage input and output properties of commands using the standard JavaBeans techniques. For example, initialize input properties with set methods (like setCheckingAccount) and retrieve output properties with get methods (like getCheckingAccount). The get methods do not work until after the execute method for the command has been called.
Related tasks
Implementing command interfaces
Reference topic