1

根据 Cairngorm 架构,我们总是在每个服务的每个命令类中都有一个故障处理程序。

我们如何创建一个类来处理所有服务的故障处理程序事件。

4

2 回答 2

1

创建一个基类,您可以从中扩展所有其他类,将故障处理程序放在那里。如:FaultHandlerCairngormCommand 扩展 SequenceCommand 实现 IResponder

[基础命令.as]

public class BaseCommand extends SequenceCommand implements IResponder
{
    public function execute( event:CairngormEvent ):void 
    {
        super.execute(event);
    }

    public function fault( info:Object ):void 
    {
        throw new Error("Generic request failure"); //or handle as you please
    }
    public function result(data:Object):void
    {
        throw new Error("The result method implementation defined in IResponder for all extensions of BaseCommand must be overrriden in any sub-class");
    }
}

[我的命令.as]

// -- no need to implement onFault in sub-class
public class MyCommand extends BaseCommand
{
    public function execute( event:Event ):void 
    {
        remoteObjectDelegate.doYourServerOperation(this);
    }
    override public function result(data:Object):void
    {
        trace("happily handling the data"); //without this override an error will be thrown so the developer will know to correct
    }
}
于 2011-03-29T15:41:25.857 回答
1

“总是有一个错误处理程序”,你的意思是合同,就像在实现一个接口时一样?

您可以编写所有其他命令类扩展的基本命令类。基类可以实现 on 错误处理程序,所有其他子类可以选择覆盖它。

public class BaseCommand implements ICommand
{
    public function execute( event:Event ):void 
    {

    }

    public function onFault( event:Event ):void 
    {

    }
}

// -- no need to implement onFault in sub-class
public class MyCommand extends BaseCommand
{
    public function execute( event:Event ):void 
    {

    }
}
于 2011-03-29T15:43:44.297 回答