WebSphere Application Server - Express for i5/OS, Version 6.1   
             オペレーティング・システム: i5/OS

             目次と検索結果のパーソナライズ化

例: 補正可能なインターフェースからのメソッドの実装

CompensableCommand インターフェースは、アプリケーション・プログラマーが実装しなければならない getCompensatingCommand メソッドを宣言します。

ModifyCheckingAccountCmdImpl クラス内の CompensableCommand インターフェースからのメソッド

以下のコード例は、ModifyCheckingAccountCmd コマンドのための実装を表示しています。 実装は単に、現行のコマンドと関連した ModifyCheckingAccountCompensatorCmd コマンドのインスタンスを戻します。

public class ModifyCheckingAccountCmdImpl extends TargetableCommandImpl
implements ModifyCheckingAccountCmd
{
...
// Method from CompensableCommand interface
public Command getCompensatingCommand() throws CommandException {
modifyCheckingAccountCompensatorCmd =
new ModifyCheckingAccountCompensatorCmd(this);
return (Command)modifyCheckingAccountCompensatorCmd;
}
}

補正コマンドの書き込み

補正可能なコマンドを使用するアプリケーションは、2 つの別 々なコマンドである、(CompensableCommand として宣言された) 基本コマンドと補正コマンドを必要とします。 サンプルのアプリケーションでは、基本コマンドは ModifyCheckingAccountCmd インターフェース内で宣言され、ModifyCheckingAccountCmdImpl クラス内に実装されます。 このコマンドも補正可能なコマンドなので、これに関連し、この処理を元に戻すために設計された第 2 のコマンドがあります。 補正可能コマンドを作成する場合は、補正コマンドも書き込む必要があります。

補正コマンドを書き込むには、元のコマンドを書き込むのとまったく 同じステップを行います。インターフェースを書き込んで、実装クラスを提供します。 より簡単に行える場合もあります。 例えば、ModifyCheckingAccountCmd を補正するコマンドは、元のコマンド に定義された以上のメソッドを必要としません。従ってインターフェースは必要ありません。 ModifyCheckingAccountCompensatorCmd と呼ばれる補正コマンドは、TargetableCommandImpl クラスを拡張したクラス内に実装されることが必要なだけです。 このクラスの条件は以下のとおりです。
  • コマンドをインスタンス化する方法を備えている。この例では コンストラクターを使用しています。
  • 次の 3 つの必須メソッドを実装している。コマンド・インターフェースの isReadyToCallExecute と reset、および TargetableCommand インターフェースの performExecute。
以下のコード例は、実装クラスの構造、その変数 (元のコマンドおよび関連する当座預金を参照する)、およびコンストラクターを表示します。 コンストラクターは単に、基本コマンドと口座への参照をインスタンス化します。
...
public class ModifyCheckingAccountCompensatorCmd extends TargetableCommandImpl
{
public ModifyCheckingAccountCmdImpl modifyCheckingAccountCmdImpl;
public CheckingAccount checkingAccount;
public ModifyCheckingAccountCompensatorCmd(
ModifyCheckingAccountCmdImpl originalCmd)
{
// Get an instance of the original command
modifyCheckingAccountCmdImpl = originalCmd;
// Get the relevant account
checkingAccount = originalCmd.getCheckingAccount();
}
// Methods from the Command and Targetable Command interfaces
....
}

performExecute メソッドは、実際の当座預金残高が、元のコマンドが戻したものと一致することを検証します。 一致している場合は、ModifyCheckingAccountCmd コマンドを使用して、現 行の残高を前に保管されていた残高と置き換えます。 最後に、補正コマンドを元に戻すことが必要となる場合に備え、最新の 残高が保管されます。 リセット・メソッドには行うべき作業はありません。

次のコード例は、継承されたメソッドの実装を表示します。 isReadyToCallExecute メソッドを実装することにより、checkingAccount 変数のインスタンス化が確実に実行されます。
...
public class ModifyCheckingAccountCompensatorCmd extends TargetableCommandImpl
{
// Variables and constructor
....
// Methods from the Command and TargetableCommand interfaces
public boolean isReadyToCallExecute() {
if (checkingAccount != null)
return true;
else
return false;
}
public void performExecute() throws CommandException
{
try {
ModifyCheckingAccountCmdImpl originalCmd =
modifyCheckingAccountCmdImpl;
// Retrieve the checking account modified by the original command
CheckingAccount checkingAccount = originalCmd.getCheckingAccount();
if (modifyCheckingAccountCmdImpl.balance ==
checkingAccount.getBalance()) {
// Reset the values on the original command
checkingAccount.setBalance(originalCmd.oldBalance);
float temp = modifyCheckingAccountCmdImpl.balance;
originalCmd.balance = originalCmd.oldBalance;
originalCmd.oldBalance = temp;
}
else {
// Balances are inconsistent, so we cannot compensate
throw new CommandException(
"Object modified since this command ran.");
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void reset() {}
}



関連タスク
コマンド・インターフェースの実装
参照トピック    

ご利用条件 | フィードバック

最終更新: Jan 21, 2008 7:05:28 PM EST
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.express.iseries.doc/info/iseriesexp/ae/rcmd_compensablecmdint.html