difference aggregation and association

Consider the distinction between object aggregation and acquaintance and how differently they manifest
themselves at compile- and run-times. Aggregation implies that one object owns or is responsible for
another object. Generally we speak of an object having or being part of another object. Aggregation implies
that an aggregate object and its owner have identical lifetimes.
Acquaintance implies that an object merely knows of another object. Sometimes acquaintance is called
"association" or the "using" relationship. Acquainted objects may request operations of each other, but they
aren't responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much
looser coupling between objects.
In our diagrams, a plain arrowhead line denotes acquaintance. An arrowhead line with a diamond at its base
denotes aggregation:
It's easy to confuse aggregation and acquaintance, because they are often implemented in the same way. In
Smalltalk, all variables are references to other objects. There's no distinction in the programming language
between aggregation and acquaintance. In C++, aggregation can be implemented by defining member
variables that are real instances, but it's more common to define them as pointers or references to instances.
Acquaintance is implemented with pointers and references as well.
Ultimately, acquaintance and aggregation are determined more by intent than by explicit language
mechanisms. The distinction may be hard to see in the compile-time structure, but it's significant.
Aggregation relationships tend to be fewer and more permanent than acquaintance. Acquaintances, in
contrast, are made and remade more frequently, sometimes existing only for the duration of an operation.
Acquaintances are more dynamic as well, making them more difficult to discern in the source code.
### Java 中的 AssociationAggregation 和 Composition 在 Java 中,Association(关联)、Aggregation(聚合)和 Composition(组合)是面向对象编程中用于描述类之间关系的重要概念。以下是它们的区别和示例。 #### 1. Association(关联) Association 是一种通用术语,表示两个类之间的关系。它可以是 Aggregation 或 Composition 的形式,也可以是简单的“使用”关系。 - **特点**: - 表示两个类之间的连接。 - 可以是一对一、一对多或多对多的关系。 - 不涉及对象的所有权或生命周期管理。 - **示例**: ```java class Teacher { String name; } class Student { String name; Teacher teacher; // Student is associated with Teacher } ``` 在这个例子中,`Student` 类与 `Teacher` 类之间存在关联关系[^1]。 --- #### 2. Aggregation(聚合) AggregationAssociation 的一种特殊形式,表示“整体-部分”关系,但部分可以独立于整体存在。 - **特点**: - 部分对象可以在多个整体对象之间共享。 - 部分对象的生命周期独立于整体对象。 - 表示弱的“拥有”关系。 - **示例**: ```java class Department { String name; } class Employee { String name; Department department; // Employee aggregates Department } ``` 在这个例子中,`Employee` 类包含一个 `Department` 对象,但 `Department` 对象可以独立存在[^2]。 --- #### 3. Composition(组合) Composition 是 Aggregation 的更严格形式,也表示“整体-部分”关系,但部分不能独立于整体存在。 - **特点**: - 部分对象的生命周期完全依赖于整体对象。 - 部分对象不能被共享。 - 表示强的“拥有”关系。 - **示例**: ```java class Heart { void beat() { System.out.println("Heart is beating"); } } class Human { Heart heart; Human() { heart = new Heart(); // Human composes Heart } void live() { heart.beat(); } } ``` 在这个例子中,`Human` 类包含一个 `Heart` 对象,且 `Heart` 对象的生命周期完全依赖于 `Human` 对象[^1]。 --- ### 关键区别总结 | 特性 | Association | Aggregation | Composition | |---------------------|--------------------------------|--------------------------------|--------------------------------| | **关系类型** | 通用关系 | 整体-部分,部分可独立存在 | 整体-部分,部分不可独立存在 | | **生命周期依赖** | 无 | 部分独立于整体 | 部分依赖于整体 | | **所有权** | 无明确所有权 | 弱所有权 | 强所有权 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值