Class design and inheritance issue in Flash AS3

I am having problems with the design of some classes. I have three classes. One superclass and two subclasses.

One subclass (AnimatedCharacter) is created using flash and used to render the object on screen. Another one (CharacterPhysics) was made by me to extend the superclass.

The problem is that the object I'm using is of type AnimatedCharacter, so I can't just put it in a variable of type CharacterPhysics.

What I've tried is a kind of Decorator pattern, providing an object of type CharacterPhysics with a reference to another object. But now I have to override all superclass methods and pass the labels to the link. Not an ideal situation.

Does anyone know how to fix this problem?

alt text http://www.freeimagehosting.net/uploads/7a95f8352c.png

0


source to share


3 answers


I don't quite understand the purpose of this class structure you are describing (class names confuse me), but in general a few things come to your mind that might help you:

Almost always the best solution is to try to rethink your class model, evaluating whether you should, for example, separate the responsibilities of classes in an alternative way to make better use of inheritance and polymorphism.

"The problem is that the object I'm using is of type AnimatedCharacter, so I can't just put it into a variable of type CharacterPhysics."



If you want to put AnimatedCharacter

in a type variable CharacterPhysics

, the former must extend the latter, or you must have a common interface (or superclass) for both and then inject the variable as such. If this is not possible, I think you should probably try to rethink and refactor your entire class structure, assuming you have a solid "object-oriented" reason for wanting to do this in the first place;).

If the above is not possible, there are other tricks you can appreciate in your context:

  • Using mixins can act as "improper multiple inheritance". Derek Wischusen has examples of how to implement them in AS3 at flexonrails.net .
  • The "view" of the implementation of the decorator pattern with flash.utils.Proxy . The problem with this approach is that you postpone a lot of error checking from compile time to runtime, but it's good that you don't have to manually write proxying implementations of all methods of the decorated object, but write just one ( callProperty()

    ).
+1


source


You can interpret a subclass as an instance of a superclass, but not a vice-class. Did you point it back?

If so, you can use:

vas cp: CharacterPhysics;



...

var ac: AnimatedCharacter = cp As AnimatedCharacter

0


source


Off the top of my head, it seems that those 2 should be interfaces that your main class implements

0


source







All Articles