spacebeam / research

Distributed Artificial Intelligence
https://spacebeam.org
Apache License 2.0
11 stars 0 forks source link

Game Object Structure: Inheritance vs Aggregation #126

Closed jchassoul closed 3 years ago

jchassoul commented 3 years ago

http://gamearchitect.net/Articles/GameObjects1.html

If moving from class hierarchies to containers of components for game objects is a trend in game development, it mirrors a broader shift in the C++ development community at large. The traditional game object hierarchy sounds like what Herb Sutter characterizes as "mid-1990s-style inheritance-heavy design" [8]. (Sutter goes on to warn against overuse of inheritance.) In Design Patterns, the gang of four recommends only two principles for object oriented design: (1) program to an interface and not to an implementation and (2) favor object composition over class inheritance [3].

Component-based game objects are cleaner and more powerful than game objects based on inheritance. Components allow for better encapsulation of functionality. And components are inherently dynamic -- they give you the power to change at runtime state which could only be changed at compile time under an inheritance-based design. The use of inheritance in implementing game object functionality is attractive, but eventually limiting. A component-based design is to be preferred.