Java 中的关联 vs. 聚合 vs. 组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14837826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Association vs. Aggregation vs. Composition in Java
提问by sriram
I'm trying to understand what these terms mean. And I came with few examples like:
我试图理解这些术语的含义。我举了几个例子,比如:
Aggregation : Facebook has auser
聚合:Facebook有一个用户
Composition : Each user in facebook has asession.
组成:facebook中的每个用户都有一个会话。
Association : People uses abrowser
关联:人们使用浏览器
But I'm getting confused with has aand uses aexamples of mine. Why can't it be User uses afacebook account or Facebook uses asession to authenticate user?
但我对has a感到困惑,并使用了我的一个例子。为什么不能是用户使用Facebook 帐户或 Facebook使用会话来验证用户?
Is that wrong in terms of OOP? Where I'm missing the concept?
就面向对象而言,这是错误的吗?我在哪里错过了这个概念?
回答by Anirudha
The uses arelationship means two things
该使用关系意味着两件事情
->both can exist independently
->两者都可以独立存在
->Data flows from the whole classifier(people) to the part classifier(browser)
->数据从整个分类器(人)流向部分分类器(浏览器)
The has arelationship means two things
该有关系意味着两件事情
->the lifetime of the part classifier(session) is dependent on the lifetime of the whole classifier(facebook)
->部分分类器(session)的生命周期取决于整个分类器(facebook)的生命周期
->data usually flows in only one direction (that is, from the whole classifier(facebook) to the part classifier(session)).
-> 数据通常只向一个方向流动(即从整个分类器(facebook)到部分分类器(session))。
回答by Carlos Gavidia-Calderon
This article from the Code Projectmakes a neat explanation of the Subject.
来自 Code Project 的这篇文章对该主题进行了巧妙的解释。
First of all there's no stone-written way to model a domain, there are several ways that are correct modeling approach to a particular problem. For example, your initial example uses a Facebook
class and in your follow-up you're now using a Facebook Account
type: using one or another (or maybe both) has impact un your model but doesn't invalidate it.
首先,没有一成不变的方法来对域进行建模,有几种方法可以对特定问题进行正确的建模。例如,您的初始示例使用一个Facebook
类,而在您的后续示例中,您现在使用的是一种Facebook Account
类型:使用一种或另一种(或可能两者)会对您的模型产生影响,但不会使其无效。
Saying that, according to the Code Project site an associationrelationship implies:
话虽如此,根据代码项目站点,关联关系意味着:
- Owner: No owner
- Life time: Have their own lifetime
- Child object: Child objects all are independent.
- 业主:无业主
- 一生:有自己的一生
- 子对象:子对象都是独立的。
With that in mind, I don't think there's an association relationship between User
and Facebook Account
since they're highly dependant from each other (assuming User refers to a Facebook user), so a compositionmight be a better relationship.
考虑到这一点,我不认为它们之间存在关联关系User
,Facebook Account
因为它们彼此高度依赖(假设用户指的是 Facebook 用户),因此组合可能是更好的关系。
I'm not sure what are you refering with your Session
class. If it's the period of time while the user is connected there is no sense in saying Facebook uses session for authentication and maybe the asociation relationship between User
and Session
can be an agregation, since a User can have several Sessions in a day an it's the owner of all of them.
我不确定你在Session
课堂上指的是什么。如果是用户连接的时间段,那么说 Facebook 使用 session 进行身份验证是没有意义的,也许User
和之间的关联关系Session
可以是一个聚合,因为一个用户一天可以有多个 Session 并且它是其所有者他们都是。
回答by Miquel
Check this wikipedia entrythat precisely discusses aggregation VS composition
检查这个维基百科条目,它精确地讨论了聚合 VS 组合
So, in Java terms, a Facebook User has a name (composition) and a Facebook User has a list of friends (Aggregation)
因此,在 Java 术语中,Facebook 用户有一个名称(组合),而 Facebook 用户有一个朋友列表(聚合)