I am rather new to OOP and I can't wrap my head around a problem I have frequently encountered by now. I hope, I understand the reasoning behind why composition is favorable to inheritance (the deeper the inheritance tree the more coupling you have and the more likely maintenance difficulties will arise in later stages). But at the same time I see no way how to avoid code duplication without making use of abstract (template) classes. So this is the specific situation I am having difficulties with at the moment (if the description is too confusing there's an UML class diagram attached in the link below...I cant upload pictures directly unfortunately, as you need 10 reputation points for that):
I have an interface i1 with the methods m1, m2, m3. It is absolutely clear that every class implementing this interface (by now there are 3 concrete classes C1, C2, C3) will have the attributes a1, a2 and a3. All these will be set in the constructor of the specific class in the same manner. Also the methods m1, m2 and m3 are implemented in the same fashion across all classes, because they delegate the method calls to one of the class attributes (a1).
Already here I cant wrap my head around how I can reasonably do without inheritance. Would it be the better solution to duplicate the constructor code across all the concrete classes c1, c2 and c3? And to implement the delegation in the methods m1, m2 and m3 every time in each new concrete class?
But it doesnt stop here. c2 and c3 do need more functionalities than given in interface i1. So there is another interface i2 with the methods m4,m5. Again both these methods will be delegated in the same manner to a1. So c2 and c3 are absolutely identical as of now. However they cant be the same class, because c3 once again needs more functionality so it implements an interface i3 with the methods (m6,m7). So if I dont use inheritance the classes c2 and c3 will have 5 methods which will be duplicated, but if used inheritance for the whole design, I will already have reached a somehwat deeper inheritance level, because there will be an abstract class for the classes c1, c2 and c3 (where the constructor code will be implemented and the methods m1,m2 and m3) and one abstract class for the classes c2 and c3 where the methods m4 and m5 will be implemented.
I found some languages have things called mixins or traits. The language I am working in (ABAP) doesnt have these. But maybe I still have some fundamental problem in my understanding.
Any help is very appreciated. Thank you!
Aucun commentaire:
Enregistrer un commentaire