赞成组合而不是继承[英] Favor composition over inheritance

本文是小编为大家收集整理的关于赞成组合而不是继承的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

偏爱继承的组成

是非常流行的短语.我读了几篇文章,最后每篇文章都说

当类之间存在纯IS-A关系时,请使用继承.

一个示例来自本文:

在 Apple 和水果之间存在明确的关系,即Apple is-A水果组合)以显示继承实现时的陷阱.

我在这里变得有些困惑,说明

的含义是什么

当类之间存在纯IS-A关系时,请使用继承.

确实在继承上使用构图意味着始终尝试即使 也存在纯粹的IS-A关系,并且仅适用于那些情况构图没有意义?

推荐答案

当您使用继承从超类重复使用代码,而不是覆盖方法并定义另一种多态性行为时,通常表明您应该使用构图而不是继承.

java.util.Properties类是对继承不良使用的一个很好的例子.它不是使用使用 a hashtable存储其属性,而是扩展了 hashtable,以重复使用其方法并避免使用委托书重新实现其中的一些.

其他推荐答案

我认为这是面向对象设计中最讨论的点之一.正如文章所建议的那样,构图始终优先于继承.这并不意味着您绝不应该使用继承.您应该在更有意义的地方(可能有争议的).

使用组合有许多优点,其中几个是:

  • 您将完全控制您的实现.即,您只能公开打算暴露的方法.
  • 超级类中的任何更改都可以通过仅在课堂中修改来屏蔽.使用您的类的任何客户类,不必进行修改.
  • 允许您在要加载超级类(Lazy Loading)
  • 时控制

其他推荐答案

我想一个很好的指南是:

当存在IS-A关系时,请使用继承.否则,请使用 组成.

其原因与面向对象设计的另一个概念 - 多态性有关.多态性是许多OOP语言的特征,只要第一个类是第二个子类,就可以使用一个对象代替另一个对象.

要说明,想象一下您是否有一种动物的功能.当您使用继承时,只有一个功能:

void feed( Animal a );

多态性向我们保证,我们投入的任何动物子类都将被接受.否则,我们将被迫为每种类型编写一个功能.我认为这要大于其缺点的好处(例如,封装减少).

如果没有IS-A关系,我认为多态性不是很有效.因此,最好使用组合并在类之间增强了封装/隔离.

本文地址:https://www.itbaoku.cn/post/627511.html

问题描述

Favor composition over inheritance

is very popular phrase. I read several articles and at the end each article says

use inheritance when there is pure IS-A relationship between classes.

An example from this article:

Here between Apple and Fruit there is clear IS-A relationship i.e Apple IS-A Fruit, yet the author has also shown it as Apple HAS-A Fruit (composition) to show the pitfall when implemented with inheritance.

I became somewhat confused here that what is the meaning of statement

use inheritance when there is pure IS-A relationship between classes.

Does using composition over inheritance mean that always try to apply composition even if there is a pure IS-A relationship and leave inheritance only for those cases where composition does not make sense?

推荐答案

When you use inheritance to reuse code from the superclass, rather than to override methods and define another polymorphic behavior, it's often an indication that you should use composition instead of inheritance.

The java.util.Properties class is a good example of a bad use of inheritance. Rather than using a Hashtable to store its properties, it extends Hashtable, in order to reuse its methods and to avoid reimplementing some of them using delegation.

其他推荐答案

I think this is one of the most discussed point in Object Oriented design. As suggested in the article, composition is always preferred over inheritance. That doesn't mean that you should never use inheritance. You should where it makes more sense (which can debatable).

There are many advantages of using composition, couple of them are :

  • You will have full control of your implementations. i.e., you can expose only the methods you intend to expose.
  • any changes in the super class can be shielded by modifying only in your class. Any clients classes which uses your classes, need not make modifications.
  • Allows you to control when you want to load the super class (lazy loading)

其他推荐答案

I guess a good guideline would be:

When there is an IS-A relationship, use inheritance. Otherwise, use composition.

The reason for this is related to another concept in Object Oriented Design - polymorphism. Polymorphism is a feature of many OOP languages where an object can be used in place of another object, provided that the class of the first is a subclass of the second.

To illustrate, imagine if you have a function that takes in a type of animal. When you use inheritance, you only have one function:

void feed( Animal a );

Polymorphism assures us that any subclass of animal we put in will be accepted. Otherwise, we will be forced to write one function for each type. I think the benefits of this outweighs its disadvantages (ex. reduced encapsulation).

If there is no IS-A relationship, I think polymorphism won't be very effective. It would thus be better to use composition and have enhanced encapsulation/isolation between classes.