AS3 Multiple Inheritance/Mixins

Well, I just got done reading this post about multiple inheritance in as3 which probably should have been titled AS3 mixin technique, because that is more or less what it is, check out the wikipedia article on mixins for some history and pros and cons.

I’ll will admit straight out that I am a supporter of MI and Mixins and the like. I am not much for the aristotelian idea of the universe, sometimes A is A, and sometimes it’s both A and B and MI, when used responsibly helps you to find more elegant and concise solutions to a great many problems. Just because people use something incorrectly in some instances doesn’t make that thing inherently evil, take for instance perscription drugs, would you rather painkillers where completely banned or never prescribed just because some or even alot of people abuse them? No, a great many things when used excessively with little understanding of what it is and how it works can be called evil, but they aren’t, the people who abuse them are evil, let’s start calling a spade a spade. The problem with MI and Mixins is not that they are evil, it’s that evil people use them for nefarious purposes.

Anyway, back onto topic, I liked the guys article, and to be honest, he was pretty brave for putting it up, the minute you say something about MI, either way, a “discussion” read “flamegeddon” ensues, not always, but it’s generally a risk when you start throwing words like Inheritance around with words like Multiple.

I got to thinking about about the framework I’ve been writing, which has a Plugin Architecture that for all intents and purposes could be adapted to a multiple inheritance scheme, or to be more precise a Mixin, why a Mixin you ask? Well, because what we are creating isn’t meant to be instantiated, not in the real sense anyway, it’s a thing that adds it’s functionality to an instantiated object.

In the example given in the article, this was done via implementing an interface and then including an as3 file with just method declarations, clever, but as has been pointed out, it’s kind of dirty in the sense of most programmers would be offended by the unstructured clutter of a bunch of free floating as3.

So, here is the mixin code:

Then we create the actual mixin classes like so:

And finally, when we want to to mix in some universal functionality:

Then, somewhere in your app put:

In the above example, you can to create the Mixin as a mixin, not some instantiable class, which is alright, because then you will think a bit more of the repercussions of adding these methods to an object, which will trim out some of the ills caused by that impulse “I’ll just inherit from X”, which every programmer has had at least once in their life, and every programmer has had it bite them on the bottom at least once.

About Jason

I am a 31 year old programmer living in the south of France. I currently work actively in the fields of Ruby/PHP/Javascript and server/website administration.
This entry was posted in ActionScript 3 Tutorials, Flash/Flex, Flex/AIR Tutorials, OOP, Topics, Tutorials. Bookmark the permalink.
  • John

    This seems like good information, but please better use English. A spelling error here and there is usually not going to hurt anything, but when something is full of spelling errors, run-on sentences, etc., it is much harder and less interesting to read.

    • John

      use better English*