(7)【Command】役割と実装例
com.adobe.cairngorm.commands.ICommand;
com.adobe.cairngorm.commands.Command;
com.adobe.cairngorm.commands.SequenceCommand;
の説明です。(Cairngorm2.1)
Commandクラスは、FrontControllerによって実行されます。
従って、イベント(CairngormEvent)が送出された時に実行されるクラスと考えます。
Cairngormでは、1イベントに対して1Commandクラスを作成します。
1イベントで複数のコマンドを実行させる必要がある場合は、SequenceCommandクラスを継承してCommandクラスを作成します。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
com.adobe.cairngorm.commands.ICommand;
役割
concreteコマンドクラスのインターフェイス。
■
ICommandインターフェイスはexecute()メソッドのみを持つインターフェイスです。
FrontControllerによってexecute()メソッドが実行されます。
実装例
Commandクラスからビジネスロジックを呼び出します。
ビジネスロジックではServiceを使用してサーバーへアクセスするので、Responderインターフェイスも実装します。
package sample.command { import mx.rpc.IResponder; import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent; import sample.event.LoginEvent; import sample.model.LoginModelLocator; public class LoginCommand implements ICommand, IResponder { // ModelLocatorのインスタンスを取得 private var model : LoginModelLocator = LoginModelLocator.getInstance(); /** * 実際の実行処理 */ public function execute(cairngormEvent:CairngormEvent):void { var event:LoginEvent = cairngormEvent as LoginEvent; var delegate:LoginDelegate = new LoginDelegate(this); delegate.doLogin(event.accountVo); } /** * Serviceを実行する場合などはIResponderを実装するので * result,faultを実装する。 */ public function result(data:Object):void{ // Serviceクラスの実行に成功したときの処理などを記述 var buff:String = String(event.result); model.message = "ようこそ" + buff + "さん。"; } /** * Serviceを実行する場合などはIResponderを実装するので * result,faultを実装する。 */ public function fault( info:Object) : void{ // Serviceクラスの実行が失敗したときの処理などを記述 } } }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
com.adobe.cairngorm.commands.Command;
■
ICommandインターフェイスを継承したインターフェイス。特になにも定義していません。
Cairngorm2.1から非推奨となりました。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
com.adobe.cairngorm.commands.SequenceCommand;
役割
一度に複数のコマンドを実行するためのコマンドクラス。
■
ICommandを実装したクラス。プロパティとしてnextEventを持ち、次回のイベントを保持することが可能です。
次回のイベントの実行はexecuteNextCommand()メソッドにて実装されており、
同メソッド内でイベント送出( CairngormEventDispatcher.getInstance().dispatchEvent( nextEvent );)を行っています。
詳細はBlog – estafa en líneaにて紹介されています。
実装例
ログインサンプルでは使用しませんが、以下に実装例を示します。
// [クラスの宣言]SequenceCommandを継承します。(Responderの実装は任意)
public class LoginCommand extends SequenceCommand implements Responder
// [executeNextCommand()を実行]onResult内にて次のコマンドを実行させる
// ここではビジネスロジックの実行結果が"1"だった場合、nextCommandを実行する
public function onResult(event:* = null):void { var buff:String = String(event.result); model.resutlCount = buff+"件削除しました"; if ( buff == "1" ){ executeNextCommand(); } }
// [executeNextCommand()を実装]
// executeNextCommand()をオーバーライドする。
public override function executeNextCommand() : void { var buff : LoginVo = new LoginVO(); var searchEvent : AccountSearchEvent = new AccountSearchEvent(buff); // Dispatch the event. nextEvent = searchEvent; super.executeNextCommand(); // Clear the event. nextEvent = null; }
次回は、【Responder】役割と実装例です。