Класс можно сделать и не статичным, просто, на мой взгляд, так удобнее запускать из любого места. Да и делать как-то иначе == писать больше кода. На счет lock/unlock - не совсем очевидно получается. Что будет если вызвать unlock до того как был вызван lock? В предолагаемом мной подходе есть 2 основные мысли:
1) optional pooling
2) контроль над использованием команд вне пула.
Правда, второй пункт в пример не попал =) Доработанынй вариант выглядит так:
Код AS3:
package
{
import flash.utils.Dictionary;
import flash.utils.describeType;
import flash.utils.getQualifiedClassName;
/**
* Global command runner
*/
public class Command
{
public function Command()
{
super();
}
private static const _classList:Dictionary = new Dictionary();
public static function run(commandClass:Class, ...args):void
{
if (!commandClass) throw new ArgumentError("Parameter must be not-null!");
if (!(commandClass in _classList))
{
var className:String = getQualifiedClassName(Command);
var description = describeType(commandClass);
var isInheritedCommand:Boolean = Boolean(description..extendsClass.(@type == className));
if (!isInheritedCommand) throw new Error("Only Command-inherited classes are allowed!");
_classList[commandClass] = new Array();
}
var pool:Array = _classList[commandClass] as Array;
var command:Command = (pool.length ? pool.pop() : new commandClass()) as Command;
command._pool = pool;
command._finalized = false;
command.start(args);
}
private var _pool:Array;
private var _finalized:Boolean;
private function start(args:Array):void
{
if (_finalized) throw new Error("Unable to run finalized command!");
this.run.apply(this, args);
}
/**
* Call this method to return this command instance to associated pool
*/
protected final function finalize():void
{
if (_finalized) throw new Error("Command already is finalized!");
_finalized = true;
_pool.push(this);
}
/**
* Run command with specified arguments
*/
protected function run(...args):void
{
throw new Error("Methot-override is supposed here!");
}
}
}