❔ Inheritance or composition?
For example, blender, C4D or any 3D modelling program would have a scene with a hierarchy of objects (e.g. cube, mesh, etc). These would obviously need a position associated with them... but would it be a better idea to use composition such that you have a
TransformationInfo
object stored as a field (or in a dictionary maybe, similar to what unity does with components), which stores the position, scale and rotation data?
Another example that really made me think of this question is premiere pro, specifically video clips. Video clips are obviously going to have some sort of manually controllable position, and also a scale value (and an origin point to assist the scale). But the way premiere pro presents that data is in the effects panel as the "Motion" effect, making me think that internally premiere pro stores all that data in an effect instead of in the clip class or struct or however they do it. If they wrote it using C structs I could understand because audio clips don't really need a position. However if it were C++, would it not be a better idea to have VideoClipObject
which stores that size and scale data, and also extends ClipObject
which is the base for every clip? Or does their way of having (I imagine) a single clip class/struct, and just tieing all the video/audio/effect data in effects, make it a better design?3 Replies
I think there is no clear winner, it depends on the use case and both have advantages and disadvantages. I personally try to avoid inheritance (especially multiple levels) and rather rely on composition. I would try it out in your specific use case (build a little prototype)
I was thinking of scrapping inheritance a little bit too. I use MVVM in my apps a lot and it would be handy if I could just carry around composite components (like
ITransformationInfo
) which I can pass to a model so that it can modify a ViewModel without even realising it. I could just make my view model inherit that, but it would looks weird if there was like 100 interfaces being implemented lolWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.