聚合vs组合vs关联vs直接关联 [英] Aggregation vs Composition vs Association vs Direct Association

查看:278
本文介绍了聚合vs组合vs关联vs直接关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在回顾我在面向对象编程中的知识.在类之间的关系主题下,我遇到了一些对我来说有点模棱两可的关系.

I am reviewing my knowledge in object-oriented programming. Under the relationship between classes topic, I have encountered some relationships which are a bit ambiguous to me.

我知道依赖项"uses-a"和继承项"is-a",但是我对聚合,组合,关联和直接关联有点不熟悉;同样,它们中的哪一个是具有"关系.有些人将聚合"与关联"互换使用.

I know dependency "uses-a" and inheritance "is-a" but I'm a bit unfamiliar with Aggregation, Composition, Association and Direct Association; also, which of them is "has-a" relationship. Some use Aggregation interchangeably with Association.

什么是直接关联?另外,什么是成分?在UML图中,代表它们的箭头是不同的.如果您能为我解决这些问题,我将非常感谢.

What is Direct Association? Also, what is Composition? In UML diagrams, the arrows that represents them are different. I would be really thankful if you could clear these things out for me.

推荐答案

请注意,关联"定义有不同的解释.我的以下观点很大程度上取决于您在Oracle认证书和学习指南中所读到的内容.

Please note that there are different interpretations of the "association" definitions. My views below are heavily based on what you would read in Oracle Certification books and study guides.

方法内的用法 ,其签名或作为返回值.它实际上不是对特定对象的引用.

A usage inside a method, its signature or as a return value. It's not really a reference to a specific object.

示例:我将汽车停在车库中.

所谓的"强关系":链接对象的实例化通常很难 编码在对象的构造函数中.不能从 在对象之外. (组成不能是多对多 关系.)

A so-called "STRONG relationship": The instantiation of the linked object is often hard coded inside the constructor of the object. It cannot be set from outside the object. (Composition cannot be a many-to-many relationship.)

示例:房屋由石头组成.

这是"弱关系".这些对象可以独立生活,通常可以使用setter或其他方式注入依赖对象.

This is a "WEAK relationships". The objects can live independent and there are usually setters or other ways to inject the dependent objects.

示例:一辆汽车可以有乘客.

非常类似于直接关联.这也是具有独立对象的"弱关系".但是,这里的关联对象是包含对象的关键部分.

Very similar to a Direct association. It's also a "WEAK relationship" with independent objects. However here the associated objects are a crucial part of the containing object.

示例:一辆汽车应该装有轮胎.

注意:直接关联和聚合关联通常都被概括为关联".差别很小.

这篇关于聚合vs组合vs关联vs直接关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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