为什么继承是强耦合的,因为组合在Java中松散耦合? [英] why inheritence is strongly coupled where as composition is loosely coupled in Java?

查看:249
本文介绍了为什么继承是强耦合的,因为组合在Java中松散耦合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经听到这个在设计模式中一次又一次地赞成组合超过遗产。引用的一些原因是

I have heard this favor composition over inheritence again and again in design patterns. some of the reasons cited for this are

1)Inheritance is strongly coupled where as composition is loosely coupled
2) Inheritance is compile time determined where as composition is run-time
3)Inheritance breaks encapsulation where as composition does not
4) anything else I am not aware of

对于初学者,对我来说,继承和组合对于上述几点有所不同,这是非常好的。我已经阅读了各种SO链接来谈论他们,但是通过关于这些要点的例子,对于Java初学者来说将是非常好的。

It would be great for beginners like to me follow these by illustrations as how inheritance and composition differs with respect to above points. I have read various SO links that talks about them, but walk through examples on these key points would be great for Java beginners.

我认为了解差异不仅仅是记住积分。

I think it is very crucial to understand the difference clearly than just memorizing the points.

感谢您的帮助

推荐答案

p>对于初学者来说,好的问题大,我想我应该首先提醒继承和组合,然后去解释什么是正确的喜欢组合继承的意思。


遗传与遗传

Nice question and big for a beginner, I think I should first remind inheritance and composition, and then go to explain what is exactly Favor Composition over Inheritance does means.

prose and cons of inheritance :

优点:


  • 动态绑定和多态性的主要优点之一是
    ,它们可以帮助代码更容易更改

  • 新的实现很容易,因为大多数都是继承的,简单到
    修改或扩展实现被重用。

<强>缺点:


  • 打破封装,因为它暴露了一个子类实现

    其超类的细节

  • 白盒子重新使用,因为超级课程的内部细节往往是
    可见的细分。

  • 子类可以如果执行
    超类更改,则必须更改。执行从超类继承的实现可以在运行时
    不被更改。

  • Breaks encapsulation, since it exposes a subclass to implementation
    details of its super-class
  • White-box reuse, since internal details of super-classes are often visible to subleases.
  • Subclasses may have to be changed if the implementation of the superclass changes Implementations inherited from superclasses can not be changed at runtime.

关于这个问题:

About the question:


继承是强耦合的,因为组合松散耦合

Inheritance is strongly coupled where as composition is loosely coupled

继承会带给你紧耦合,只需要改变一个基类就可以打破许多小孩的课程。


但是什么时候使用和如何检测我们需要继承或组合?

仅当满足以下所有条件(Coad规则)时才使用继承:

Inheritance will bring you tight coupling, simply one change to base class can break many child classes.

But when to use and how detect we need inheritance or composition?
Use inheritance only when all of the following criteria are satisfied (Coad's Rule):


  1. 一个子类表达是一个规范而不是是由扮演的角色。

  2. 子类的实例不需要成为另一个类的对象

  3. 一个子类扩展,而不是覆盖或取消
    的超级类的职责

  4. 一个子类不扩展什么只是一个
    实用程序类的功能

  5. 对于实际问题域中的类,该子类专门化
    a角色,事务或设备

  1. A subclass expresses is a special kind of and not is a role played by a.
  2. An instance of a subclass never needs to become an object of another class
  3. A subclass extends, rather than overrides or nullifies, the responsibilities of its super-class
  4. A subclass does not extend the capabilities of what is merely a utility class
  5. For a class in the actual Problem Domain, the subclass specializes a role, transaction or device




继承是编译时间确定组合运行时的位置

Inheritance is compile time determined where as composition is run-time

编译时,您的基类代码将添加到每个子类。

When compiled, your base class codes will be added to every child class.


继承破坏封装,其中组合不

Inheritance breaks encapsulation where as composition does not

是的,看到继承的缺点。

Yes .see the disadvantage of inheritance .

底线是

确保inheri tance模型是一个关系
我的主要指导思想是,只有当一个子类是一个超类时才应该使用继承。在上面的例子中,苹果可能是一个水果,所以我会倾向于使用继承。
一个重要的问题,当你认为你有一个关系时,问自己是否是 - 一个关系在整个应用程序的整个生命周期中都是不变的,运气就是代码的生命周期。例如,您可能会认为员工是一个人,当真正的员工代表一个人在一段时间内扮演的角色时。如果这个人失业了怎么办?如果该人既是员工又是主管,该怎么办?这样的永恒是 - 一个关系通常应该用组合来建模。

Make sure inheritance models the is-a relationship My main guiding philosophy is that inheritance should be used only when a subclass is-a superclass. In the example above, an Apple likely is-a Fruit, so I would be inclined to use inheritance. An important question to ask yourself when you think you have an is-a relationship is whether that is-a relationship will be constant throughout the lifetime of the application and, with luck, the lifecycle of the code. For example, you might think that an Employee is-a Person, when really Employee represents a role that a Person plays part of the time. What if the person becomes unemployed? What if the person is both an Employee and a Supervisor? Such impermanent is-a relationships should usually be modelled with composition.

不要使用继承来获取代码重用
如果所有你真正想要的是重用代码,没有一个关系,使用组合。

Don't use inheritance just to get code reuse If all you really want is to reuse code and there is no is-a relationship in sight, use composition.

不要使用继承来获得多态
如果你真正想要的是多态,但没有自然的关系,使用组合与接口。


喜欢组合继承:)

我直接从 javaworld

这篇关于为什么继承是强耦合的,因为组合在Java中松散耦合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆