Do you extend or compose Sprite, MovieClip and DisplayObject?

I got such a great response from my last help request that I'm posting another:

On my journey to try and be a better developer, I'm questioning some of the fundamental ways I work and organise code. Like most Flash developers (I think), I normally extend Sprite or MovieClip when I want to control something on the stage. But would I be better off creating a class that composes (i.e. controls via a reference) a Sprite or MovieClip instead? What are the advantages and disadvantages? Which are you doing? Leave comments please!

Comments

Iain said…
I just came across this - worth a read on the topic...

http://www.moock.org/blog/archives/000248.html
Squize said…
I use composition, purely 'cause in my head it feels cluttered extending a sprite / mc, in that my class will inherit a lot of things it won't use.

Also it feels like I'm more in control that way, that the display object is part of a whole, rather than being the whole/

Wow, what a great post :S Sorry, tired and the words just aren't coming tonight.
felix said…
I always use composition when possible. This way you can separate the 'business logic' of the class from the MovieClip's properties. Also extending MovieClip means your classes are dynamic which is not usually good.

On a related note I try not to use the Library Symbol Properties to link a symbol to a class. IMO it's cleaner to keep the linkage ids in the AS file.
Iain said…
thanks for your input felix and squize! that's 2 votes for composition so far. Looks like this one's not going my way ;)

@felix - need to correct you on that thing about classes extending MovieClip being dynamic - not true, unless you explicitly use the dynamic tag. So actually if you compose a MovieClip you've have more dynamic objects in your code.
Ickydime said…
I got to go against Squize and Felix on this one... thems fightin words! ;)

I use a mix, but a majority of my classes extend Sprite or MovieClip. MovieClip if its art brought in from an Assets.swf and Flash IDE. Sprite if I am dynamically creating it.

I find it easier to not have to re-invent the wheel by creating getter/setter functions for everything. If I want to restrict or customize anything specific then I will override that function.

So basically I combine my controller class with my view in most cases and only use the basic class/object for my data and util classes.

Right or wrong... that is what I do. Thanks for the discussion Iain.
Squize said…
I think we need to take this outside to the car park mate ;)

I'm not sure why you'd need a load of getter / setters. Maybe on the controller classes ( Such as a BaddieHandler class that loops through all them bad boys and handles triggering them and houseKeeping ) but when you're in those child classes, the player bullets, the particles, they're kinda self supportive.
For the most part they need an init(), mainloop() and houseKeeping() and that's it.

It is all kinda moot though, I think it boils down to personal preference.
It's like the first Bond you saw usually ends up being your favourite, even if it was Roger Moore.
Iain said…
Thanks guys, thanks for completely contradictory answers! What do I do now!? One thing I like about composition of MovieClips is not having a million things on the autocompletion menu. I like the writing less code of extending though. Hmmm.
Andre Anaya said…
I usually extend a Sprite or MovieClip in my classes when the instance will be on a display list. If not, I think it's better compose your class with a Sprite.

However, you have to keep in mind that compose instead of extend use more memory and is one more step when you need to acess some property or method of the Sprite. It's a small step, but it's one more thing to slow down your app.
yezzer said…
As the OOP mantra goes: "Favour composition over inheritance".

With regards to the display list, when I started doing AS3 I found I was using inheritance far too much.
Over the last year I've found that composition seems to work much better. Why? It's tricky to precisely define, it just seems more logical and cleaner.
Bet that doesn't help at all :)
aaulia said…
Since you ask for opinion, here is mine :). I favor composition, why ? Because it's cleaner and (for me anyway) it force me to think about how I design my classes and not to hack something up for quick fix. For example, a plane class doesn't need most of the Sprite/Bitmap/MovieClip method and properties on it, they are 'high level' classes, they doesn't supposed to expose those stuff. And the because you are not allowed to access things that is used to be in the Sprite/MC/Bitmap class, you ough to think about what your class are doing with it (Sprite/MC/Bitmap) and it prevent you from doing a hack because you don't have direct access to Sprite/MC/Bitmap anymore :)
Will S said…
We can go back and forth but really this will boil down to a matter that simply depends on the case. Where does the class fit conceptually, with AS3s "framework" or the one you make specific to your needs and tastes? If you are extending the movieclip or sprite -merely- because it will be a visible object, then that's not very OOP, and probably what adobe had in mind when they decided to implement interfaces - their way of adhering to polymorphism (another buzz word we should all be learning). On the flip-side, just because we all "accidentally always" used inheritance till we knew better, doesn't mean there's not a time and place for it.
Og2t said…
Hmmm, it's all very interesting. I must admit I use inheritance at 99% and I am happy with this, sub classes are quite clean that way, I don't need to reference all the methods that I need to use.

I feel that I somehow must be missing the good sides of composition, or do something wrong, is there any good blog post about inheritance?

Cheers,
T