什么是与面向对象设计有关的组合?[英] What is composition as it relates to object oriented design?

本文是小编为大家收集整理的关于什么是与面向对象设计有关的组合?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我听到(并在此网站上阅读)很多关于"偏爱继承的组成".

但是什么是Compositon?我从人的点了解继承:哺乳动物:动物,但我在任何地方都看不到堆肥的定义..有人可以填补我吗?

推荐答案

构图是指结合简单类型以制造更复杂的类型.在您的示例中,构图可能是:

Animal:
    Skin animalSkin
    Organs animalOrgans


Mammal::Animal: 
    Hair/fur mammalFur
    warm-blooded-based_cirulation_system heartAndStuff

Person::Mammal: 
    string firstName
    string lastName

如果您想完全构图(并摆脱所有继承),它看起来像这样:

Animal:
    Skin animalSkin
    Organs animalOrgans

Mammal:
    private Animal _animalRef
    Hair/fur mammalFur
    warm-blooded-based_cirulation_system heartAndStuff

Person:
    private Mammal _mammalRef
    string firstName
    string lastName

这种方法的优点是类型Mammal和Person不必符合其前任父母的接口.这个可能是一件好事,因为有时更改超类可能会对子类产生严重影响. 他们仍然可以通过这些课程的私人实例访问这些类的属性和行为,如果他们想揭露这些以前的裔阶级行为,他们可以简单地以公共方法包装它们.

我在此处找到了一个很好的链接,请参见: http://www.artima.com/DesignTechniques/compoinh.html

其他推荐答案

组成只是组成整体的部分.汽车有轮子,发动机和座椅.继承是一种"是"关系.组成是一种"有"的关系.

其他推荐答案

有三种方法可以为课程提供行为.您可以将该行为写入班级;您可以从具有所需行为的类中继承;或者,您可以将带有所需行为的课程纳入您的课程或成员变量.最后两种代表代码重复使用的形式,最后一种 - 组成 - 通常是首选.它实际上并没有为您的课程提供所需的行为 - 您仍然需要在现场调用该方法 - 但对您的类设计的约束更少,并且结果更易于测试,并且更易于调试代码.继承有其位置,但应优选组成.

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

问题描述

I hear (and read on this site) a lot about "favour composition over inheritance".

But what is Compositon? I understand inheritance from the point of Person : Mammal : Animal, but I can't really see the definition of Compostion anywhere.. Can somebody fill me in?

推荐答案

Composition refers to combining simple types to make more complex ones. In your example, composition could be:

Animal:
    Skin animalSkin
    Organs animalOrgans


Mammal::Animal: 
    Hair/fur mammalFur
    warm-blooded-based_cirulation_system heartAndStuff

Person::Mammal: 
    string firstName
    string lastName

If you wanted to go totally composition (and get rid of all inheritance) it would look like this:

Animal:
    Skin animalSkin
    Organs animalOrgans

Mammal:
    private Animal _animalRef
    Hair/fur mammalFur
    warm-blooded-based_cirulation_system heartAndStuff

Person:
    private Mammal _mammalRef
    string firstName
    string lastName

The advantage to this approach is that the types Mammal and Person do not have to conform to the interface of their previous parent. This could be a good thing because sometimes a change to the superclass can have serious effects on the subclasses. They still can have access to the properties and behaviours of these classes through their private instances of these classes, and if they want to expose these former-superclass behaviours, they can simply wrap them in a public method.

I found a good link with good examples here: http://www.artima.com/designtechniques/compoinh.html

其他推荐答案

Composition is simply the parts that make up the whole. A car has wheels, an engine, and seats. Inheritance is a "is a " relationship. Composition is a "has a" relationship.

其他推荐答案

There are three ways to give behavior to a class. You can write that behavior into the class; you can inherit from a class that has the desired behavior; or you can incorporate a class with the desired behavior into your class as a field, or member variable. The last two represent forms of code reuse, and the final one - composition - is generally preferred. It doesn't actually give your class the desired behavior - you still need to call the method on the field - but it puts fewer constraints on your class design and results in easier to test and easier to debug code. Inheritance has its place, but composition should be preferred.