0 Replies Latest reply on Jan 22, 2010 2:42 PM by john_dot_a_at_caseydotnet

    Flex/AS3 Best way to construct a derived class instance from an existing base class instance

    john_dot_a_at_caseydotnet

      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.