Creating a Class Dynamically in Flex Based on the Name

We're working on a Flex project at AboutWeb and wanted to use an object factory. Nic Tunney created an object factory in ColdFusion that uses an XML file for configuration. We were wondering if we could do the same thing in Flex. In order to do this we needed to be able to create a class dynamically based on a string value. A bit of research turned up the GetDefinitionByName function. Here's a sample of how it works:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="addButton()">
   <mx:Script>
      <![CDATA[
         import flash.utils.getDefinitionByName;
      
         private function addButton():void{
            var className:String="mx.controls.Button";
            var ClassReference:Class = getDefinitionByName(className) as Class;
            var instance:Object = new ClassReference();
         
            instance.label="Test"
            this.addChild(DisplayObject(instance));
         }
      ]]>
   </mx:Script>
</mx:Application>

Comments
Ah, the flash.utils package, one of my favorites. The object introspection stuff is pretty cool too.
# Posted By Alex Hofsteede | 8/13/07 5:45 AM
It's worth noting that you can only get classes which have been compiled into the application. The easiest way to include a class you don't otherwise use is to make a variable of that type which you never use.

Eg. var example:MyObject

That will make the compiler include the MyObject class within the swf
# Posted By Tony Fendall | 8/13/07 6:38 AM
Tony pointed this out already: you can quickly run into linking issues, esp. since factories like this are most used around interfaces like delegates, where your outside app is only concerned with something like the interface a delegate implements. In such a situation, you'd probably never instantiate the concrete delegate, meaning it'd never get linked and compiled into your app...

I ran into this when writing the Fling library (http://fling.riaforge.com), which serves as an "object factory" / IoC container, in the style of Spring / ColdSpring. Fling works around this by having you create a "configuration" application (compiles to a .swf) that references your concrete classes via binding. Your external app then loads the configuration swf into the factory, making the concrete classes available at runtime through factory methods (e.g., var bean:BeanInterface = beanFactory.getBean("ConcreteBean") as BeanInterface).

I'd recommend checking out Fling, it may be just the factory you're looking for.

-Joe
# Posted By Joe Rinehart | 8/13/07 11:39 AM
I found this to be very interesting



http://freeskullylayouts.com
# Posted By Aaliyah | 12/30/07 3:30 PM
Here is the article that talks about the issue we are both having. I use the update in this article.
Great guide guys keep up the good work.
Thanks for very interesting article.

Great guide guys keep up the good work.
Andrzej Filipowicz
Still life Artist, still life art
http://www.andrzejfilipowicz.com
STILL LIFE PAINTING
http://www.life.e-phils.com
# Posted By Still life Artist | 1/24/08 3:49 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.004.