国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

21.7.創建Cairngorm FrontController 和ServiceLocator

2024-04-27 13:52:29
字體:
來源:轉載
供稿:網友
21.7.1.問題
我需要擴展Cairngorm FrontController類并創建自定義的事件類型。
21.7.2.解決方案
創建一個擴展自Cairngorm FrontController的類。在構造函數中,使用方法addCommand把CairngormEvent類型和command類關聯起來。ServiceLocator應該擴展Cairngorm ServiceLocator類并要包含你的程序需要的所有服務。
21.7.3.討論
FrontController類在Cairngorm結構中扮演著重要角色:把CairngormEvent和command聯系起來。它是通過方法addCommand完成這一工作的,看看FrontController基類中的這個方法:
+展開
-ActionScript
public function addCommand(commandName : String, commandRef :Class, useWeakReference : Boolean= true): void{
if(commands[commandName ]!= null)
throw new CairngormError(CairngormMessageCodes.COMMAND_ALREADY_REGISTERED, commandName );
commands[commandName ]= commandRef;
CairngormEventDispatcher.getInstance().addEventListener(commandName, executeCommand, false
, 0, useWeakReference );
}

Command類是以事件名稱作為鍵值存儲起來的,當發送這個事件時,就會調用這個command。CairngormEventDispatcher監聽這個事件,當廣播這個事件時,FrontController使用兩個protected方法新建一個command并運行它。方法getCommand只是在命令字典中查找command并創建這個command的實例:
+展開
-ActionScript
protected function executeCommand(event : CairngormEvent ): void {
var commandToInitialise : Class = getCommand(event.type);
var commandToExecute : ICommand = new commandToInitialise();
commandToExecute.execute(event );
}
protected function getCommand(commandName : String): Class {
var command : Class = commands[commandName ];
if (command == null)
throw new CairngormError(CairngormMessageCodes.COMMAND_NOT_FOUND,commandNam );
returncommand;
}

然后會調用新command的execute方法,完成它所有的事務邏輯。下面是FrontController的一個實現:
+展開
-ActionScript
package oreilly.cookbook.cairngorm.controller {
import com.adobe.cairngorm.control.FrontController;
import oreilly.cookbook.cairngorm.commands.RecipeCommand;
import oreilly.cookbook.cairngorm.events.RecipeEvent;
public class Controller extendsFrontController {
public function Controller() {
super();
/*Command是用CairngormEvent的名字注冊的。在本例子中,RecipeEvent.GET_RECIPE會創建并運行一個新的RecipeCommand:*/
// In this simple example there is only one Command that needs to be added
// to the controller, but usually there are far more Commands to be added
// when the controller is initialized. addCommand(RecipeEvent.GET_RECIPE, RecipeCommand);
}
}
}

FrontController的其他功能隱藏在你將要擴展的類com.adobe.cairngorm.control.FrontController中。ServiceLocator類中保存了你的應用程序所需要的所有要訪問的服務的引用。它可能是HTTPService,WebService,或者RemoteObject組件。ServiceLocator使用每個服務的id訪問它們并調用它們的方法。下面例子展示了ServiceLocator是如何訪問的:
+展開
-ActionScript
ServiceLocator.getInstance().getRemoteObject("recipeService");

ServiceLocator定義了3種方法用來訪問服務:

getRemoteObject()
傳遞RemoteObject的id,并返回一個RemoteObject。

getHTTPService()
傳遞HTTPService的id,并返回一個HTTPService對象。

getWebService()
傳遞WebService的id,并返回一個WebService對象。

ServiceLocator擴展自Cairngorm ServiceLocator類,并包含應用程序的所有服務對象:
+展開
-XML
<cairngorm:ServiceLocator xmlns="*xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*">

<mx:RemoteObject id="recipeServicedestination="http://localhost/service_location/"
showBusyCursor="truesource="oreilly.cookbook.service.RecipeService"
makeObjectsBindable="true">

<mx:method name="getRecipe"/>
</mx:RemoteObject>
</cairngorm:ServiceLocator>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邯郸市| 隆尧县| 肃北| 句容市| 拜城县| 额尔古纳市| 佛山市| 英德市| 文化| 通江县| 仁化县| 叶城县| 密云县| 郑州市| 嫩江县| 萍乡市| 芦溪县| 玉田县| 同江市| 古交市| 东丰县| 翁牛特旗| 抚顺县| 霍州市| 札达县| 阜南县| 巴青县| 茂名市| 遵义市| 安泽县| 诸城市| 雷州市| 秭归县| 大英县| 广元市| 平远县| 沽源县| 安化县| 西和县| 龙游县| 改则县|