I've got a question about using "Composition over Inheritance" in game development. Let's say we have an Entity that handles both Collision and Render processes. How do we achieve this without needing a unique implementation for each process? For example, an octopus and a spaceship would likely have very different implementations for the same processes. Doesn't that defeat the whole point of modularity and making composition easier?
1 Answer
Great question! The usual guideline is that composition is for "it has a..." while inheritance is for "it is a..." In your case, Collision and Render are behaviors—both octopus and spaceship are entities that can collide and render, so you want to treat them as such in higher-level code. Inheritance works here because it allows code reuse. You also might find opportunities to use composition if certain entities share similar collision behaviors.

Interesting point! How could we blend these concepts in code? Like, if we just have an Entity class, would we use composition for easier behaviors like rendering and then have an intermediary superclass for specifics like octopus movement?