Posted By

unkiwii on 02/04/12


Tagged

object as3 builder


Versions (?)

Object Builder


 / Published in: ActionScript 3
 

URL: https://github.com/unkiwii/Random-Stuff/blob/master/as3/src/ObjectBuilder.as

This is a helper class to create objects like if you were calling "apply" on the constructor object. An example:

class MyObject { public function MyObject(a, b, c) { ... } }

// normal object creation var a:MyObject = new MyObject('a', 'b', 'c');

// using ObjectBuilder var p:Array = ['a', 'b', 'c']; var b:MyOBject = ObjectBuilder.buildFromArray(MyObject, p);

  1. package
  2. {
  3. public class ObjectBuilder
  4. {
  5. static private const builders:Array = [
  6. build0p, build1p, build2p, build3p, build4p,
  7. build5p, build6p, build7p, build8p, build9p
  8. ];
  9.  
  10. static public function buildFromParams(ObjectClass:Class, ...params):* {
  11. return builders[params.length](ObjectClass, params);
  12. }
  13.  
  14. static public function buildFromArray(ObjectClass:Class, params:Array):* {
  15. return builders[params.length](ObjectClass, params);
  16. }
  17.  
  18. static private function build0p(ObjectClass:Class, p:Array):* { return new ObjectClass(); }
  19. static private function build1p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0]); }
  20. static private function build2p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1]); }
  21. static private function build3p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1], p[2]); }
  22. static private function build4p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[1], p[1], p[2], p[3]); }
  23. static private function build5p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1], p[2], p[3], p[4]); }
  24. static private function build6p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1], p[2], p[3], p[4], p[5]); }
  25. static private function build7p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1], p[2], p[3], p[4], p[5], p[6]); }
  26. static private function build8p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); }
  27. static private function build9p(ObjectClass:Class, p:Array):* { return new ObjectClass(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]); }
  28. }
  29. }

Report this snippet  

You need to login to post a comment.