This conundrum is making me feel like a newbie. I’m trying to follow the standard advice of limiting the amount of duplicate code.
Let’s say we have a fairly involved abstract class as a starting point.
Let’s say it has methods aa(), bb(), cc(), dd(), ee().
Now, lets say that of the subclasses being made, some use identical code override aa(), others use identical code to override bb() and others use identical code to override cc().
One solution would be to make three abstract subclasses to match the three common cases. Then the overriding code is only written once, one for each case.
My conundrum, though, is that the subclasses that require overrides of aa(), bb(), and/or cc() may do so in any combination. For example one requires the new aa() and bb() but not the new cc(), or another requires the new bb() or cc() but not aa(). All in all, there are (2^3)-1 possible combinations of these method overrides. Writing each abstract subclass with duplicate code for the various method overrides is what I am hoping to avoid. (Am also worried about the growth pattern getting worse if yet more common overrides prove useful and independent.)
At that point, maybe it makes sense to just store the overriding methods as text file templates and paste them in when making the subclasses directly from the abstract class.
Seems like this would be an issue that has been solved many times in the past, but I am failing to think of keywords to search for how others have handled this or if there is a design pattern for it.
Anyone else familiar with how this might be best implemented? Or do I just live with the duplicate code.