Показать сообщение отдельно
Старый 29.10.2012, 18:43
gloomyBrain вне форума Посмотреть профиль Отправить личное сообщение для gloomyBrain Найти все сообщения от gloomyBrain
  № 6  
Ответить с цитированием
gloomyBrain
 
Аватар для gloomyBrain

блогер
Регистрация: Mar 2008
Адрес: РФ, Санкт-Петербург
Сообщений: 2,272
Записей в блоге: 5
Отправить сообщение для gloomyBrain с помощью ICQ Отправить сообщение для gloomyBrain с помощью Skype™
Класс можно сделать и не статичным, просто, на мой взгляд, так удобнее запускать из любого места. Да и делать как-то иначе == писать больше кода. На счет 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!");
        }
 
    }
}
__________________
...вселенская грусть