Flex/AS3 Best way to construct a derived class instance from an existing base class instance?
Copy link to clipboard
Copied
What is the best way to handle the instantiation of a derived class from an existing base class.
I have a base class which is being created via remote_object [RemoteClass alias] from the server. I have other specialized classes that are derived from this baseclass, but serialization with the server always happens with the base class. The base class has meta data that defines what the derived class is, for example
[RemoteClass (alias="com.myco...')]
public Class Base
{
public var derivedType:String;
public function Base()
{
}
}
public Class Derived extends Base
{
public "some other data"
public function Derived()
{
}
}
In my Cairgorm command which retrieves this object from ther server I want to do this:
public function result (event: Object):void
{
var baseInstance:Base = event.result;
if (baseInstance.derivedType = "derived")
{
var derivedInstance:Derived = new Derived( baseInstance );
}
}
What is the most efficient way of doing this? It appears to me that doing a deep-copy/clone and instantiation of the derived class is pretty inefficient as far as memory allocation and data movement via the copy.

Copy link to clipboard
Copied
I am not certain i completely follow your question.
You say you have a base class holding data for the derrived type?
I would ask you to be a bit clearer on what it is you are tyring to do. Are you looking to modify behavior, clone an object, or cast an object?
it appears in your example you are tyring to modify Behavior. In that case you need no derrivitate in your base class but rather in your "Decorating class"
i would love to assist you but i need a bit more knowledge over your scenario.
What i read is that you have a sever that kicks off your program by sending in an object which could be many of specialized classes. then you for some reason need to figure out what that is in your Cairgorm method.
That is really all i understood.
But the answer to your heading "Best way to construct a derived class instance from an existing base class instance?" at run time is the decorator pattern
Copy link to clipboard
Copied
Thanks for the assistance. Let me try to clarify.
MY UI requires a number of composite classes. The individual components of the composite classes are being transfered to/from the server at different times depending upone which component has changed state. The construction of the composite classes from the base class happens in my clients business logic.
Composition happens in a derived class; but server syncronization happens using the base class. When I recieve the object from Blazeds through the remote object event, it is in the form of the base class. I then need to instantiate the derived class and copy the elements of the base class into it (for later composite construction). And likewise when sending the base class back to the server, I need to upcast the derived class to its base class. But in this case just a mere upcast does not work. I actually need to create a new base class and copy the attrbutes into it. I believe this is limitation of how remoting works on Flex/AS3.
My question is, what is the best way to turn my base class into it's derived class so further composite construction can take place. The way I am currently doing it is to create a load method on the base class, that takes the base class as on argument. The load function, copies all of the instance attribute references from the base class to the target class.
public Class Base
{
public function Base()
{
}
public function load(fromClass:Base)
{ // copy the references for all of the instance attributes from the fromClass to this class }
}
Then, after I recieve the base class from the server. I create a new derived class and pass the base class into the load function like this:
for (var i:int=0; i < event.result.length; i++) {
var derived:Derived = new Derived();
derived.load(event.result);
The drawbacks of this approach is that it now requires 2 extra instance creations per object serialization. One on recieving the object from the server and one sending it to the server. I assume copying references are pretty efficient. But, there is probably some GC issues. The worst of it is in code maintenance. The load function now has to be manually maintained and kept in sync with the server class.
It would be interesting to hear how others have solved this problem. The server side is an existing application with around 2M LOC, so changing the code on the server is a non-starter.
Thanks for your help.

