❔ How can I reuse code without inheritance?
I'm trying to learn composition over inheritance, and so far so good, but the problem is that quickly there is a lot of complex code reuse, just take a look at both of these constructors, so much things I have to keep consistent between two classes.
In case of inheritance I would just call the base class constructor, but what are my other possibilities here? The more of them, the better. Thanks.
8 Replies
you shouldn't spurn inheritance all the time
just know what it's good for
inheritance is best for is-a relationships, which your snippet seems to be a classic example of
(a player is-a entity, an enemy is-a entity)
composition is imo most useful for bits of actual logic and functionality - services - which you can then glue together as needed
not necessarily for modelling entities
to give a more pragmatic answer, though
you could factor out the repeated code into a third class
and use that in both to calculate positions, spawn offsets, etc
Yeah, I understand that, I'm trying to test the limits, this is mostly a learning project and I'm trying to find more alternatives.
understood, that's a worthwhile approach
anyway, yeah. the composition approach is always just 'make another class to encapsulate the logic and get it through DI', basically
hmm, do you have any more specific example? A lot of open-source projects I've checked use inheritance a lot. I wonder how that could look like.
My thought right now is something like this
IConstructor.Construct()
, which would be called in both constructors, but idk if that's something that devs actually use and I wonder if it has a better name.i mean like this
forgive the formatting
Yeah, that makes sense, thanks
np
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.