I'm looking for some architecture advise.
My AIR application has the ability to render certain kinds of data, each
of which has a unique string descriptor (e.g. "red"). I would like to
make it easy for others to add renderers for other kinds of data. To
accomplish this, I created a renderer directory (Object) where the data
descriptor is a key to the class path for the renderer and I planned to
make extensive use of dynamic instantiation - when I ran into "red"
data, I could just look up the "red" renderer, instantiate it, etc. My
thinking was that the new developer would just need to create a renderer
that implemented the requiste interface, create an entry in the
directory, and would need very minimal understanding of the rest of the
architecture to get their data to render. But I ran into the problem
described here:
http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-i\
n-actionscript-30/
<http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-\
in-actionscript-30/%20>
If you don't want to read it all, basically you need to make sure the
referenced class has been loaded into your swf before you try to
reference it (at least, that's my understanding). So, in my directory, I
created something that creates and then destroys (for memory
conservation) an instance of each renderer, and now my directory looks
something like this:
public class RendererDirectory {
private var dir:Object { red:"com.blah.renderers.MyRenderer",
blue:"com.joedata.renderers.BlueRenderer", ...};
// to make sure everything is in .swf
private static function kluge():void {
var a:Array = [new MyRenderer(), new BlueRenderer(), ...];
a = null;
}
public function getRenderer(datakey:String):IMyInterface {
// all error checking omitted for brevity
var classPath:String = dir[datakey];
var classRef:Class = getDefinitionByName(classPath) as Class;
return new classRef();
}
}
I'm looking for advise on whether there is a better way to accomplish my
big picture goal (ease of extensibility) or improve on my current scheme
in some way.
TIA!