🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Inheritance vs. Aggregation

Published September 02, 2008
Advertisement
I Choose Aggregation!All of the Hieroglyph versions that I have worked on over the years are based on a variant of the Wild Magic object/entity design. This design uses a class to represent a 3D 'entity', and then uses a subclass to implement a 3D 'node' class, allowing for scene graph style connectivity between scene objects.

After working on the first iteration of my engine for about three years, I had more or less relied on inheritance to extend the object hierarchy and implement new functionality. For instance, to add a skybox to the engine I would need to create a subclass of the Entity3D class which forced it's position to be centered on the current camera location and render a cube surrounding the entire environment. This worked OK, but ended up with many, many different classes in a given scene graph for an application. There is also the issue of overloading a function from the original Entity3D class that shouldn't be completely replaced - such as an update function that writes the current local and world matrices for a given entity. Controllers Are BetterTo help in this situation, controllers can be used to implement special behavior without modifying or extending the Entity3D class. However, with controllers it can become tedious to communicate between the application, the entity, the controllers on an entity and how the rest of the entities on the scene graph talk to each other. Clearly there must be something better out there... Aggregates Are Even BetterI am now in the process of switching to using 'macro' objects which aggregate the entity object, it's controllers, and additional special state information for new style objects. The distinction seems to be subtle, but this design solves quite a few issues and makes using the rendering capabilities of the entity system very diversified.

In fact, this small change in design has allowed me to implement the full blown 3D UI with my existing rendering system without any special case code and still allowing for an object oriented approach of creating UI components. This will look alot better once I can take some screen shots, but it is going to be worth the extra effort to change things over. Definitely more to come soon!
0 likes 3 comments

Comments

FReY
September 03, 2008 05:07 AM
Jason Z
That's pretty cool - it seems like this is more of a trend than I had imagined. After looking around, I see that KHawk's journal also mentions a few links to similar types of architectures. There must be some truth to this concept!
September 03, 2008 05:32 AM
jollyjeffers
The last few big bits of code I've written have been quite heavy on object composition rather than inheritance. When implemented via design-by-contract I've found it to be very expressive and of a much higher quality than simple heirarchies. I don't think I'll go back.


Jack
September 03, 2008 08:57 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement