This content has been marked as final. Show 4 replies
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?
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?
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?
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: