看到没有产生代码重复的(更深的)继承树避免(更深的)继承树
我对 OOP 相当陌生,我无法集中注意力解决我现在经常遇到的问题。我希望,我理解为什么组合有利于继承背后的原因(继承树越深,耦合度越高,后期就越有可能出现维护困难)。 但与此同时,我看不出如何在不使用抽象(模板)类的情况下避免代码重复。 所以这就是我目前遇到的困难的具体情况(如果描述太混乱,下面的链接中附有一个 UML 类图...不幸的是,我无法直接上传图片,因为你需要 10 个声誉点为此):
我有一个接口 i1,其方法为 m1、m2、m3。 很明显,每个实现该接口的类(现在有 3 个具体类 C1、C2、C3)都将具有属性 a1、a2 和 a3。所有这些都将以相同的方式在特定类的构造函数中设置。此外,方法 m1、m2 和 m3 在所有类中以相同的方式实现,因为它们将方法调用委托给类属性之一 (a1)。
在这里,我已经无法思考如何在没有继承的情况下合理地做到这一点。在所有具体类 c1、c2 和 c3 中复制构造函数代码是否是更好的解决方案?并在每个新的具体类中每次都在方法 m1、m2 和 m3 中实现委托?
但它并不止于此。 c2 和 c3 确实需要比接口 i1 中给出的功能更多的功能。因此还有另一个接口 i2,其方法为 m4、m5。同样,这两个方法都将以相同的方式委托给 a1。所以到目前为止 c2 和 c3 完全相同。然而它们不能是同一个类,因为 c3 再次需要更多功能,因此它使用方法 (m6,m7) 实现接口 i3。 因此,如果我不使用继承,类 c2 和 c3 将有 5 个重复的方法,但如果在整个设计中使用继承,我将已经达到更深层次的继承级别,因为这些类将有一个抽象类c1、c2 和 c3(其中将实现构造函数代码以及方法 m1、m2 和 m3)以及类 c2 和 c3 的一个抽象类,其中将实现方法 m4 和 m5。
我发现有些语言有称为 mixins 或 Trait 的东西。我正在使用的语言(ABAP)没有这些。但也许我的理解仍然存在一些根本性的问题。
非常感谢任何帮助。谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
描述有点令人困惑,所以如果我错了,请纠正我。
似乎您对每个接口都有一个实现。只需在您想要具有此功能的每个类中撰写此实现即可。例如,
c1
将构成i1
和c2
的实现i2 。当然,实现本身将获得相同的参数。例如,为了方便起见,例如,
c1
作为i1
的实现,您可以使用用户定义的转换函数进行操作:其中
m_i1 < /代码>是
i1
的合成实现。这样,继承是深度1。此外,它可以很容易地扩展和更改。例如,也许
c1
希望i1
然后c2
的其他实现很容易实现。The description is a bit confusing so correct me if I've got it wrong.
It seems like you have a single implementation of each one of the interfaces. Simply compose this implementation in every class that you want to have this functionality. For example,
C1
will compose the implementation ofi1
andC2
will compose the implementation ofi1
andi2
. The implementation itself will, of course, gets in the ctor the same parameters.If for convenience you wish to treat, for example,
C1
as an implementation ofi1
, you may do it with a user-defined conversion function:Where
m_i1
is the composited implementation ofi1
.This way the inheritance is of depth 1. Moreover it's can be easily extended and changed. For example, maybe
C1
would like a different implementation ofi1
thenC2
it will be easily achieved.