This should do the job.
1. ClassFactory to create classes specified by strings.
<code>
class ClassFactory
{
public static function create(className:String):Object
{
// Split the class name into its parts
var parts:Array = className.split(".");
// a variable to build a reference to the class
var classRef:Object = _global;
// Iterate over the parts, digging deeper each time
// into the namespace heirarchy
for(var n=0;n < parts.length;n++)
{
classRef = classRef[parts[n]];
}
// Check that the class exists
if(classRef == undefined)
{
throw new ClassCreationException("Class not found : " +
className);
}
else
{
return new classRef();
}
}
}
</code>
2. The Class Creation Exception, nothing special :)
<code>
class ClassCreationException extends Error
{
public function ClassCreationException(msg:String)
{
message = msg;
}
}
</code>
and some test code to check it works :
<code>
var className:String = "com.test.MyClass";
import com.test.MyClass;
var c:MyClass;
try
{
var c:Object = ClassFactory.create(className);
}
catch(e)
{
trace(e.message);
}
</code>
of course, you need the reference to the class you want to create somewhere or
the compiler wont include it when it builds the swf, hence the
var c:MyClass line.
for me thats the worst part of the whole thing, but theres ways you can organise
this, e.g. a simple include that just lists the declarations you will need
#include "forceIncludes.as"
which just contains a list of the references
im sure there are other solutions to this little problem, if anyone else has any
bright ideas id like to know.
Of course, the other part of this is that the class factory can only return
objects that are typed as 'Object' so you lose quite a bit of type safety like
this, unless of course you are creating objects that all conform to a specific
interface. You might want to modify the class factory to add methods that return
particular types of objects with a return type set to their common interface. It
depends on what your requirements are really on how you architect this part.
hope that all makes sense.
thanks,
Martin
franto wrote:
Hi all,
maybe it is easy, maybe not, but i cant figure it out now, and i need it :)
when i got class e,g pkg1.pkg2.pkg3.className
i can make new instance in this way
new pkg1.pkg2.pkg3.className()
but i want to make it from string
this dont work of course
_global['pkg1.pkg2.pkg3.className']();
but this work:
_global['pkg1']['pkg2']['pkg3']['className']();
can this done automatcally? i want jsut read string from XML and
create new class instance,
but i cant do it now.
Any help is appreciated :)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Franto
http://blog.franto.com
http://www.flashcoders.sk
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
--
Martin Wood
http://relivethefuture.com/choronzon
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders