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

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

21.7.創(chuàng)建Cairngorm FrontController 和ServiceLocator

2024-04-27 13:52:29
字體:
供稿:網(wǎng)友
21.7.1.問題
我需要擴展Cairngorm FrontController類并創(chuàng)建自定義的事件類型。
21.7.2.解決方案
創(chuàng)建一個擴展自Cairngorm FrontController的類。在構(gòu)造函數(shù)中,使用方法addCommand把CairngormEvent類型和command類關(guān)聯(lián)起來。ServiceLocator應(yīng)該擴展Cairngorm ServiceLocator類并要包含你的程序需要的所有服務(wù)。
21.7.3.討論
FrontController類在Cairngorm結(jié)構(gòu)中扮演著重要角色:把CairngormEvent和command聯(lián)系起來。它是通過方法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類是以事件名稱作為鍵值存儲起來的,當(dāng)發(fā)送這個事件時,就會調(diào)用這個command。CairngormEventDispatcher監(jiān)聽這個事件,當(dāng)廣播這個事件時,F(xiàn)rontController使用兩個protected方法新建一個command并運行它。方法getCommand只是在命令字典中查找command并創(chuàng)建這個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;
}

然后會調(diào)用新command的execute方法,完成它所有的事務(wù)邏輯。下面是FrontController的一個實現(xiàn):
+展開
-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會創(chuàng)建并運行一個新的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類中保存了你的應(yīng)用程序所需要的所有要訪問的服務(wù)的引用。它可能是HTTPService,WebService,或者RemoteObject組件。ServiceLocator使用每個服務(wù)的id訪問它們并調(diào)用它們的方法。下面例子展示了ServiceLocator是如何訪問的:
+展開
-ActionScript
ServiceLocator.getInstance().getRemoteObject("recipeService");

ServiceLocator定義了3種方法用來訪問服務(wù):

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

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

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

ServiceLocator擴展自Cairngorm ServiceLocator類,并包含應(yīng)用程序的所有服務(wù)對象:
+展開
-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>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 海丰县| 新竹市| 凤山市| 东海县| 汾阳市| 武宁县| 文昌市| 周至县| 合作市| 昆明市| 秭归县| 宜宾市| 汨罗市| 花垣县| 信丰县| 奎屯市| 垣曲县| 湛江市| 阿坝县| 元氏县| 上栗县| 兰州市| 长子县| 彰武县| 临西县| 额敏县| 济阳县| 周宁县| 元江| 民权县| 高碑店市| 长白| 大冶市| 海伦市| 绵阳市| 南木林县| 昌吉市| 益阳市| 梨树县| 马山县| 宁德市|