zoukankan      html  css  js  c++  java
  • Cairngorm2 中SequenceCommand用法

    Cairngorm2 中SequenceCommand用法

    概述:

    The SequenceCommand is provided as a "psuedo-abstract" (since ActionScript has no real concept of abstract classes) base-class that can be extended when you wish to chain commands together for a single user-gesture, or establish some simple form of decision-based workflow.

     

    By extending SequenceCommand, you can specify the event that should be broadcast to the controller (causing another command execution without a further user-gesture) when the current command has completed execution.

     

    For a command implementing the Responder interface, you may choose to sequence a subsequent command on successful completion of the command, in the onResult() handler, or on failure of the command in the onFault() method.

     

    For commands that do not implement the Responder interface, you can simply chain commands by causing the sequenced command to be invoked as the last action in your command's execute() method.

     

    一般用法:

    1. SequenceCommand实例构造函数中设置nextEvent

    2. 对于实现IResponder的Command,在onResult()或onFault()中调用executeNextCommand()来执行下一chain command

    3. 对于未实现IResponder的Command,在execute()方法的最后调用executeNextCommand()来执行下一chain command

     

    对于复杂链式命令,封装ChainEvent后的实现:

    1. 定义一个ChainEvent,所有需要按顺序执行的事件都继承自该事件。

    package xxx.events

    {

    import com.adobe.cairngorm.control.CairngormEvent;

    import flash.events.Event;

    /**

    *

    * ChainEvent,所有需要按顺序执行的事件都继承自该事件

    *

    * @see com.adobe.cairngorm.control.CairngormEvent

    *

    */

    public class ChainEvent extends CairngormEvent

    {

    public static const CHAIN_EVENT:String = "xxx.events.ChainEvent";

     

    public var nextEvent:ChainEvent;

    public var myValue:String;   //测试值

     

    public function ChainEvent()

    {

    super( CHAIN_EVENT );

    }

     

    public override function clone():Event

    {

    var res:ChainEvent = new ChainEvent();

    res.nextEvent = nextEvent;

    res.myValue = myValue;

    return res;

    }

     

    }

    }

    2. 建立ChainEvent工厂类

    package xxx.events.utils

    {

    import xxx.events.ChainEvent;

    /**

    *

    * ChainEventFactory,chainEvent静态工厂类

    *

    */

    public final class ChainEventFactory

    {

    public static function createChainEvent(evts:Array):ChainEvent

    {

    if ( evts.length < 1 )

    return null;

     

    var res:ChainEvent = evts[ 0 ] as ChainEvent;

    for (var i:int=0; i<evts.length; i++ )

    {

    if (i<evts.length-1)

    {

    var event:ChainEvent = evts[ i ] as ChainEvent;

    var nextEvent:ChainEvent = evts[ i+1 ] as ChainEvent;

    event.nextEvent = nextEvent;

    }

    }

    return res;

    }

    }

    }

    3. 实现Command

    package xxx.test.command

    {

    import com.adobe.cairngorm.commands.SequenceCommand;

    import com.adobe.cairngorm.control.CairngormEvent;

    import xxx.events.ChainEvent;

    public final class ChainCommand extends SequenceCommand

    {

    public override function execute(event:CairngormEvent):void

    {

    var chainEvent:ChainEvent = event as ChainEvent;

    trace(chainEvent.myValue);

    if (chainEvent.nextEvent != null)

    {

    this.nextEvent = chainEvent.nextEvent;

    this.executeNextCommand();

    }

    }

    }

    }

    4. 链式ChainEvent派发

    var firstChainEvent:ChainEvent = new ChainEvent();

    var secondChainEvent:ChainEvent = new ChainEvent();

    var thirdChainEvent:ChainEvent = new ChainEvent();

    firstChainEvent.myValue = "firstChainEvent";

    secondChainEvent.myValue = "secondChainEvent";

    thirdChainEvent.myValue = "thirdChainEvent";

    var chainEvent:ChainEvent = ChainEventFactory.createChainEvent([firstChainEvent,secondChainEvent,thirdChainEvent]);

    chainEvent.dispatch();

     

    最后结果输出:

    firstChainEvent

    secondChainEvent

    thirdChainEvent

  • 相关阅读:
    Android Studio库依赖问题
    Android学习笔记View的工作原理
    专用服务器模式&共享服务器模式
    Linux命令学习总结:shutdown
    Linux查看设置系统时区
    ORA-01950: no privileges on tablespace xxxx
    complex(x):创建一个复数
    python常用函数之--求绝对值函数:abs(x)
    python学习链接:
    requirejs学习博客址分享
  • 原文地址:https://www.cnblogs.com/god_bless_you/p/2085268.html
Copyright © 2011-2022 走看看