21.7.创建Cairngorm FrontController 和ServiceLocator

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>

在前面四节中,列出了Cairngorm 小型结构的主要组件,并介绍了每个组件在程序中的作用。

更多信息请访问:http://www.adobe.com/go/cairngorm.

加支付宝好友偷能量挖...


评论(0)网络
阅读(148)喜欢(0)flash/flex/fcs/AIR