View Full Version : Inheritance vs composition
EightySeven
11-13-2008, 05:09 PM
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
rawmantick
11-14-2008, 06:01 AM
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
snapple
11-25-2008, 04:44 PM
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
maskedMan
11-25-2008, 05:33 PM
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.
NickZA
12-15-2008, 04:33 PM
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:
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.
senocular
12-15-2008, 04:47 PM
I tend to use inheritence where I can (if it applies) since its less work : )
jeffturner
02-05-2009, 08:23 AM
In simple words inheritance manes to inherit some properties from other,
yell0wdart
02-05-2009, 03:17 PM
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.
maskedMan
02-05-2009, 03:57 PM
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.
memoorthy
02-06-2009, 01:40 PM
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!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.