ActionScript: package level introspection? Or some other plug-in boot scheme?

I am coding delightful business logic, and I came to this situation:

There are many rules ( ruleA, ruleBetc.) that conform to standard patterns. I created an interface Ruleand implemented it in ruleA, ruleBetc.

So now I just need a way to download them.

Now I am using a map:

var rules:Object = {
    ruleA: new RuleA(),
    ruleB: new RuleB(),
   ...
};

Then call it like this:

function doStuff(whichRule:String, someData:Object):* {
    return rules[whichRule].applyTo(someData);
}

Now the rules follow the standard naming scheme, and they are all part of the same package ( foo.rules) ... Is there a way to load the rules without saving them in some kind of list?

Thanks!

. , , . :

function loadRule(name:String):Rule {
    import foo.rules;
    var rule:Class = foo.rules[name];
    return new rule();
}

, , foo.rules... , .

+3
5

flash.utils.getDefinitionByName(className);

def getRule(someValue: String) : Class{
    return getDefinitionByName("mybusiness.rules.Rule" + someValue);
}

, .

, , , , swf. , , . , .

: AS3, Rule :

def getRule(someValue: String) : Function{
    return new Rule()["theRuleName"];
}

, , , .

+3

, , , simply impossible... ... , ...

:

ActionScript ... , , swf... ...

, , , script, , - - ... , Array,

[Rule1, Rule2,...RuleN]

, ... , ( ), getDefinitionByName, ...

, , , , ... ...

- , ... , , , , ... ...

:

class NameIsRule implements IRule {
    private var _name:String;
    public function NameIsRule(name:String) {
        this._name = name;
    }
    public function applyTo(target:Object):* {
        return target.hasOwnProperty("name") && (target.name == this._name)
    }
}

rules = {
    isCalledJim : new NameIsRule("Jim"),
    isCalledJohn : new NameIsRule("John")
}

( , , )

, ...

package {
    public class RuleMap {
        private var _rules:Object = {}
        public function RuleMap(init:Object = null) {
            for (var name:String in init) 
                this.addRule(name, init[name]);
        }
        public function addRule(id:String, rule:IRule):IRule {
            if (this.ruleExists(id))
                throw new Error("duplicate rule for id " + id);
            if (rule == nul)
                throw new ArgumentError("invalid value null for parameter rule");
            this._rules[id] = rule;
            return rule;
        }
        public function applyRule(id:String, target:Object):* {
            return this.getRule(id).applyTo(target);
        }
        public function getRule(id:String):IRule {
            if (this.ruleExists(id))
                return this._rules[id];
            else 
                throw new Error("no rule for id " + id);
        }
        public function ruleExists(id:String):Boolean {
            return this._rules.hasOwnProperty(id);
        }
    }
}

verbous (, ), , , AVM2 ( , ... , )...

, , , ... .....

AS3, , , ... AS3 ... , ...

(, ), IMHO, , ... , - ... ...;)

Greetz

back2dos

+5

Factory Patteren, . - :

public class RuleFactory
{
   public function create(ruleID : String) : IRule
   {
      switch (ruleID)
      {
         case "ruleA":
            new RuleA();
            break;
         case "ruleB":
            new RuleB();
            break;
      }
   }

}

-

function doStuff(whichRule:String, someData:Object):* 
{
   var ruleFactory : RuleFactory = new RuleFactory();
   var rule : IRule = ruleFactory.create(whichRule);

   return rule.applyTo(someData);
}

, , , , , , .

0

:

  • XML/JSON , .

  • javascript ExternalInterface.

  • SWF .

0

? , ActionScript ( Ruleby, ), , .

0

Source: https://habr.com/ru/post/1713375/


All Articles