4 Replies Latest reply on Jul 28, 2007 8:54 PM by MatthewFord

    [AS3] Losing my religion: subclassing vs. composition

    MatthewFord
      I have been until now a fervent disciple of compositing a graphic instance into a class in every case, rather than making my class extend MovieClip. Admittedly I was a piker at AS2 and only now that I am cranking on AS3 do I consider myself worthy to call myself a beginner OOP programmer. But I bought into all the reasons, and if I were still making AS2 apps, I might remain in that camp.

      But lately my devotion has been shaken and now I am seriously considering extending (and then subclassing from) MovieClip or Sprite when making any class which has a visual component. Whereas before I would always compose in a MC instance, almost never make my class inherit from anything but my own superclass, and implement interfaces for polymorphism, now I think I will extend, although still implement interfaces for the usual other reasons. I also still plan to be a composition devotee in all other ways-- I like composition more than inheritance. Also I would not extend MC for classes which have no graphical component, such as game logic components. (I am making games and other interactive entertainment apps, for in-browser distribution.)

      Can I please be illuminated by better coders than I as to the wisdom or folly of my change of heart?

      Here are a few reasons why my mind is changing; please feel free to refute or support any of these:

      --Changes between AS2 and AS3 make the downside of subclassing MC/Sprite smaller than before. I'll let far more informed people come up with good examples, but one seems to be the explicit nature of adding an object to the display list-- the overhead hit of being an MC is small (nonexistent?) if it is not on the list.

      --Colin Moock, in his seminal "Essential ActionScript 2" book, advocated composition-- see his large MVC example in which his view class, although nearly entirely visual in function, did not extend MC but rather composited MC. However, in Moock's equally impressive AS3 book, all his examples now seem to use extension. I really admire Moock so this gives me a new perspective.

      --All the Adobe best practices and examples use extension, as do nearly all code samples I see in other helpful forums. I am not one to blindly run with the crowd but I think I'd get better feedback on my code from my peers if I follow suit.

      --All the AS3 Flash components seem to use extension, and indeed, AS3 itself is heavily inheritance-based rather than composition-based, for understandable reasons.

      I am probably missing a lot and mis-stating much of what is there but hopefully you get the idea.

      So once again I ask my better peers: what are now the arguments for and against extending MC or Sprite for one's visually represented classes in AS3? Any observations would be much appreciated.
        • 1. Re: [AS3] Losing my religion: subclassing vs. composition
          MatthewFord Level 1
          Here is a bit more concrete example:

          In Moock's AS2 book he has an extensive example of the MVC pattern involving, what else, a clock. In his code his Clock view does *not* extend MovieClip (Sprite not having been available in AS2), which is in keeping with his general admonition to compose a graphic element into a class rather than extend one. His view's constructor takes an MC as an argument, and the view attaches its MC instance (which contains all the clock visuals) to the passed MC. The view object is not itself attached, because it is not an MC. The view class contains an MC which ends up getting attached. To change the XY of the view would be to ask the view to change its MC instance's XY. Pure composition.

          In Moock's AS3 book he has an extensive "Virtual Zoo" example which among other things uses an MVC-ish structure, in which the view and controller are the same class. If anything, this would make composing in the view's root graphic element even more sensible than his AS2 example. The View does not really fit the definition "is-a Sprite"; it also is a view manager and controller. But, contrary to his AS2 stance and in keeping with all the other examples in his AS3 book, Moock's view class *extends* Sprite, and is added as a child by the main class. Though the view does happen to contain objects which are also made from Sprite-extending classes (pictures of the zoo animal and the like), the view class does not contain its root sprite the way the AS2 example contained its root MC. The view *is* a Sprite. To change the XY of the view would be to simply set its inherited XY properties from the outside, as any Sprite would be positioned. So though Moock still is a devotee of composition in every other way, on this one point he seems to have decided to use inheritance and take a path different from his AS2 book.

          And I have seen most other AS3 code examples follow suit. The Lott AS3 book does, and all the Adobe examples do. So forgive me for having my faith shaken a wee bit.

          Though I remain convinced of the rule to favour composition, in this particular set of cases I am considering routinely using inheritance, even though I easily *could* use composition. It just seems to be messier, denser, and longer to use composition *in this set of cases*. So is my reasoning good, faulty, or does it simply betray deep cluelessness about OOP?
          • 2. Re: [AS3] Losing my religion: subclassing vs. composition
            Rothrock Level 5
            I think you are a geek! :) (That is meant all in the best fun.)

            You are far too worried about it. If you know enough to ask the question you probably know enough to make the decision yourself.

            I take the part where he talks about (paraphrasing here) "Does your custom class HAVE a movieclip (or whatever class is in question) or IS IT a new kind of movieclip?"

            Also if you expect you won't need all the methods of the parent class, then don't extend.

            I'm not familiar with the AS3 book yet, but if a custom class is going to be used as a Document class then it needs to extend either Sprite or MovieClip. I can't remember the reason why, but Keith Peters talks about the reason in his excellent book. So perhaps that is what is going on here?
            • 3. Re: [AS3] Losing my religion: subclassing vs. composition
              MatthewFord Level 1
              Heh, thanks Rothrock... to be called a geek on a Flash forum just gives me even more geek points. Score! :)

              It's true about the Document class. You don't absolutely need to use a Doc class... in my fervor I started off instanciating my main class in the timeline script and passing it an MC from the stage. Once I accepted that it was kind of silly not to use the Document class, and therefore I needed to make my main extend Sprite, it kind of cracked my resolve, and like most cases of losing one's religion, a crack quickly develops into a chasm. :) Besides, frame scripts seem even more repugnant than extension!

              I guess I can either convince myself that my entity class for example is-a MovieClip.. I position it, make it play animations, and could scale and rotate it if I wanted. Sure, it does a lot more, and there are MC methods I don't need, but it's probably putting too fine a point on it to worry.

              Thanks for the reassurance (and the geek points)... more opinions welcome but I'm become more and more certain I should usually extend. Anyone else want to chime in?
              • 4. Re: [AS3] Losing my religion: subclassing vs. composition
                MatthewFord Level 1
                For anyone who was following this breathtaking saga, Colin Moock generously answered it via his blog when I wrote to him. What a guy!

                Check out the answer here:
                http://www.moock.org/blog/archives/000248.html