Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-13-2008, 06:09 PM   #1
EightySeven
Senior Member
 
Join Date: Aug 2008
Posts: 102
Default Inheritance vs composition

I was wondering what everyone's preference is in flash, I use AS3 and since I'm just making small apps and games I mainly use Composition, but I was wondering how many of you use inheritance.

I can see using inheritance if your doing a larger project where architecture is important, but I'm not doing those at the moment.

Thoughts and opinions very welcome, I'd like to hear others thoughts and preferences
EightySeven is offline   Reply With Quote
Old 11-14-2008, 07:01 AM   #2
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Novosibirsk, Russia
Posts: 1,713
Default

I almost always use inheritance, though this way is not a panacea for any problem. This is just a way I got used to. And I think it requres less coding.

But sometimes it's realy neccessary to use composition. For example you create a class which is generally inherited from another class. But some of its methods are a little bit another. Their difference cannot be programmed overridingly. Sometimes for example methods have just another return types - you cannot just override them. So composition with both implementing some new interface will be the best solution.

Actualy any class relationship, I think, has its own right to exist and has its own place to be used.

It realy depends on a sitation. I think using only this, or only that is a bad practice either...

That's just what I think, and how I see things. This opinion helps me... for a while
__________________
fly high
rawmantick is offline   Reply With Quote
Old 11-25-2008, 05:44 PM   #3
snapple
member
 
snapple's Avatar
 
Join Date: Feb 2002
Location: London
Posts: 2,413
Send a message via MSN to snapple
Default

On the whole, composition is a better practice to follow, because:

Inheritance = compile-time.
Composition = run-time.

That said, it is a massive subject and completely depends on your project, code library and requirements - and probably 50 other things too.

Regards, snapple
snapple is offline   Reply With Quote
Old 11-25-2008, 06:33 PM   #4
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 697
Default

It really depends on what you're trying to do. Some solutions lend themselves to inheritance, and others to composition. In reality though, you'll find that both are useful in conjunction with each other.

You might want to use composition if you are trying to extend or improve Flash's bizarre collection of Sound related classes, as they are disparate and truly need to be corralled together if they are to be used meaningfully. Done correctly, this in turn can lead you to use your improved sound player in other composites with relative ease and without a great deal of pain and suffering.

On the other hand, inheritance is a great way to extend the functionality of standard buttons and movieclips by altering default functions to provide template hooks or to tell movieclip subclasses that they should dispatch events in new and interesting situations.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'

Last edited by maskedMan; 11-25-2008 at 06:37 PM..
maskedMan is offline   Reply With Quote
Old 12-15-2008, 05:33 PM   #5
NickZA
Member
 
NickZA's Avatar
 
Join Date: Aug 2007
Location: Ascot, UK
Posts: 67
Default

The Gang of Four recommended composition over inheritance in most things.

But one of their own exceptions worth noting (and stated explicitly by them) is that of the Template Method pattern, where the entire pattern relies on the concept of inheritance.

In general, though:

Quote:
The disadvantage of class inheritance is that the subclass becomes dependent on the parent class implementation. This makes it harder to reuse the subclass, especially if part of the inherited implementation is no longer desirable. According to [16], "inheritance breaks encapsulation." One way around this problem is to only inherit from abstract classes. Another problem with class inheritance is that the implementation inherited from a parent class cannot be changed at run-time.
...from http://brighton.ncsa.uiuc.edu/prajlich/T/node14.html

EDIT: Just to add, If you want to have some regard for those who are going to be maintaining your code (if that won't be you) then you should probably respect the principles of encapsulation.
__________________
www.visualharmonics.co.uk

Last edited by NickZA; 12-16-2008 at 08:59 AM..
NickZA is offline   Reply With Quote
Old 12-15-2008, 05:47 PM   #6
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,761
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

I tend to use inheritence where I can (if it applies) since its less work : )
__________________
(6)
senocular is offline   Reply With Quote
Old 02-05-2009, 09:23 AM   #7
jeffturner
Guest
 
Posts: n/a
Default hey

In simple words inheritance manes to inherit some properties from other,
  Reply With Quote
Old 02-05-2009, 04:17 PM   #8
yell0wdart
jordanrift.com
 
Join Date: Sep 2007
Location: Phoenix, AZ
Posts: 297
Default

I tend to try not to get too hung up on one vs the other. I use them both *where it makes sense to use them*. I tend to favor composition where I can, as it leads to looser coupling and fewer dependencies. But either, used properly, will get the job done.
__________________

bad developer

Jordan Rift
yell0wdart is offline   Reply With Quote
Old 02-05-2009, 04:57 PM   #9
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 697
Default

Nice to revisit this topic after so long.

As time has progressed, I have found myself doing one thing less often and that is assigning a Class or a Base Class to a library instance. Since I have so little control over the library at my job, trying to assign linkages to the library symbols is extremely counter-productive because I can't predict what will change. Therefore, I have ceased using inheritance to extend movieclips and instead use composition by creating decorators and wrappers for those movieclips.

Working by myself, I had no issues with extending movieclip since I always knew how things would be structured but now I find that I only ever give a library item a class name when I know I have to instantiate it at runtime. Even though it's mostly due to external factors (ie: other team members), I chalk another one up for composition.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 02-06-2009, 02:40 PM   #10
memoorthy
Tech Strat
 
Join Date: Mar 2004
Location: Dubai, UAE
Posts: 12
Send a message via Yahoo to memoorthy
Smile Good talk though

I use more of inheritance and comparitively less of composition. In the end it is all decided on what kind of project you are working on!
memoorthy is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mxml inheritance Elephantium Flex 2 & 3 2 04-23-2008 08:24 PM
Delegation, Aggregation, and Composition....oh my! Flash Gordon ActionScript 3.0 14 03-26-2008 04:18 AM
Classes and inheritance kdawg ActionScript 3.0 5 09-04-2007 09:46 PM
Controlling object timeline w/ composition diogobaeder ActionScript 2.0 0 04-21-2007 03:56 AM
OOP Theory: Composition vs. Inheritance LostInRecursion ActionScript 2.0 2 12-29-2005 02:17 AM


All times are GMT. The time now is 06:38 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.