6 Replies Latest reply on Jan 21, 2010 11:03 AM by msakrejda

    Passing args to a constructor through ClassFactory.newInstance?

    rickcr

      I know how I can create a new instance of a class using getDefinitionByName and using ClassFactory.newInstance, and I know I can also add properties to be set by adding them to the classFactory properties field. However, what I do not know how to do is how do I make sure a constructor is called with certain arguments?

       

      In my case I'm creating an instance of classes (by a string name) that all have a constructor signature like the following (just as an example):

       

      public function Foo(boo:String) {

          this.boo = boo;

          init();

      }

       

       

      Do I need to extend ClassFactory and override newInstance() and handle it there somehow?

       

      Thanks

        • 1. Re: Passing args to a constructor through ClassFactory.newInstance?
          msakrejda Level 4

          You'd need to write a custom IFactory--I don't think ClassFactory can do what you want. That said, this shouldn't be hard:


          public class ConstructorFactory implements IFactory {

            private var _class:Class;

            public var properties:Object;

            public var constructorArgs:Array;

            public function newInstance():* {

              var result:*;

              // Unfortunatley, there's no such thing as apply for a constructor, so you have to resort to this ugly hack:

              switch (constructorArgs.length) {

                case 0: result = new _class(); break;

                case 1: result = new _class(constructorArgs[0]); break;

                case 2: result = new _class(constructorArgs[0], constructorArgs[1]); break;

                ...

              }

              for (var key:String in properties) {

                result[key] = properties[key];  

              }

              return result;

            }

          }

          • 2. Re: Passing args to a constructor through ClassFactory.newInstance?
            msakrejda Level 4

            Oops--you'll also need to expose a constructor like this:

             

            public function ConstructorFactory(clazz:Class) {

              _class = clazz;

            }

            • 3. Re: Passing args to a constructor through ClassFactory.newInstance?
              rickcr Level 1

              Thanks msakreejda! Just so I have the concept right, I'd end up swapping out using a ClassFactory and replace it with a Factory similar to the one you demonstrated (assuming maybe setting the Class instance and constructor args through this new factory's constructor? So an implementation might look like....

               

               

              var classRef:Class = getDefinitionByName("FooBar") as Class;

              //pass Class instance and list of contructor args to ConstructorFactory
              var factory:ConstructorFactory = new ConstructorFactory(classRef, {"foo"} );

              var myCreatedInstance = factory.newInstance();

              http://forums.adobe.com/people/msakrejda

              • 4. Re: Passing args to a constructor through ClassFactory.newInstance?
                msakrejda Level 4

                Yep, something like that. With my implementation above (which has constructor args as a property not a constructor argument), it would be something like:

                 

                var classRef:Class = getDefinitionByName("FooBar") as Class;

                // pass Class instance to ConstructorFactory
                var factory:ConstructorFactory = new ConstructorFactory(classRef)

                // set constructor args

                factory.constructorArgs = [ 'foo' ];

                // set properties, if desired, just as in ClassFactory

                factory.properties = {

                  bar: 'baz',

                  quux: 'xyzzy'

                };

                var myCreatedInstance = factory.newInstance();

                • 5. Re: Passing args to a constructor through ClassFactory.newInstance?
                  rickcr Level 1

                  Awesome stuff thanks so much!

                   

                  I think this would be great to be part of the core

                  ClassFactory api. If you add constructor args like properties, it attemps to use them, otherwise procedes as normal with the default constructor. Not so fond of the ugly args array hack though either. Although a reasonable max args length provided by default would be fine (once you go over 5 or so anyway, you should consider refactoring your constructor args to take a single object that holds the props anyway.)

                  • 6. Re: Passing args to a constructor through ClassFactory.newInstance?
                    msakrejda Level 4

                    Yep, the constructor thing is ugly, but in practice, you can hide that ugliness fairly well. The Mate framework uses constructor arg arrays all over the place, but there's just one place in the code that has a switch like that (with support for up to ten args!), and it works fine. I think it's the kind of thing you have to be pragmatic about.