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

  • 相关阅读:
    【Python3网络爬虫开发实战】 1-开发环境配置
    Elasticsearch 基本介绍及其与 Python 的对接实现
    深度学习 GPU环境 Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.
    React组件方法中为什么要绑定this
    中级前端开发推荐书籍
    20万行代码,搞得定不?
    华为云数据库TaurusDB性能挑战赛,50万奖金等你来拿!
    00036_private
    使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
    http304状态码缓存设置问题
  • 原文地址:https://www.cnblogs.com/god_bless_you/p/2085268.html
Copyright © 2011-2022 走看看