概要

Association, Aggregation and Composition are terms that represent relationships among objects. They are very basic stuff of Object Oriented Programming.

关联

Association is a relationship among the objects. Association is "*a*" relationship among objects. In Association, the relationship among the objects determine what an object instance can cause another to perform an action on its behalf. We can also say that an association defines the multiplicity among the objects. We can define a one-to-one, one-to-many, many-to-one and many-to-many relationship among objects. Association is a more general term to define a relationship among objects. Association means that an object "uses" another object.
For example Managers and Employees, multiple employees may be associated with a single manager and a single employee may be associated with multiple managers.

聚合

Aggregation is a special type of Association. Aggregation is "*the*" relationship among objects. We can say it is a direct association among the objects. In Aggregation, the direction specifies which object contains the other object. There are mutual dependencies among objects.
For example, departments and employees, a department has many employees but a single employee is not associated with multiple departments.
UML Representation of Aggregation (white diamond):

The UML representation of the example above (relation between employee and department):

Here, the lives of both objects are independent of each other. That means that in this Association (Aggregation) the object has their own life cycle. Employees may exist without a department. Here, department can be called an owner object and the employee can be called a child object. The owner and child objects cannot belong to a different parent object.

组合(复合)

Composition is special type of Aggregation. It is a strong type of Aggregation. In this type of Aggregation the child object does not have their own life cycle. The child object's life depends on the parent's life cycle. Only the parent object has an independent life cycle. If we delete the parent object then the child object(s) will also be deleted. We can define the Composition as a "Part of" relationship.
For example, the company and company location, a single company has multiple locations. If we delete the company then all the company locations are automatically deleted. The company location does not have their independent life cycle, it depends on the company object's life (parent object).
UML Representation of Composition (black diamond):

UML representation of the example above (relation between Company and Company Location):

Here, the lives of both objects are not independent. The life of the company location object can be determined by the life of the company object. The company object is responsible for creating and destroying company location objects.

关联,聚合和组合三者之间的关系图


Aggregation and Composition are a special type of Association. Composition is again a special type of Aggregation. We can define Aggregation and Composition as "has a" relationships. Composition is more restrictive or more specific. In Composition, composed objects cannot exist without the other object. This type of restriction does not exist in Aggregations. In Aggregation, the existence of a composed object is optional. In Aggregation, the child object can exist beyond the life cycle of its parent whereas in Composition the child object cannot exist beyond the life cycle of its parent.
Combined example of Aggregation and Composition:


聚合VS组合

Aggregation Composition
Aggregation is a special type of Association. Composition is a special type of Aggregation.
All objects have their own life cycle. In Composition, the child object does not have their own life cycle and it depends on the parent's life cycle.
A parent class is not responsible for creating or destroying the child class. The parent class is responsible for creating or destroying the child class.
Aggregation can be described as a "Has-a" relationship. Composition can be described as a "Has-a" relationship as well as a "Part of" relationship, but here the difference is the length of the relationship among the objects.
Aggregation is a weak Association. Composition is a strong Association.
Aggregation means one object is the owner of another object. Composition means one object is contained in another object.
The direction of a relation is a requirement in both Composition and Aggregation. The direction specifies which object contains the other one.
Both have a single direction of association.
Both have a single owner.

结论

These three terms are more important in the object oriented world. They denote or represent the relations among objects. If you are confused or unable to decide whether a specific relation best describes an Association, Aggregation or Composition then it can be decribed as an Association.

引用:http://www.c-sharpcorner.com/UploadFile/ff2f08/association-aggregation-and-composition/

关联,聚合和组合(复合)--Association, Aggregation and Composition的更多相关文章

  1. UML类关系:依赖,关联,聚合和组合

    UML图示例:(可使用StartUML来画图,小巧^_^) http://www.blogjava.net/lukangping/archive/2010/08/01/327693.html 聚合:表 ...

  2. Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别

    在C++中,类与类之间的关系大概有四种,分别为继承,关联,聚合,和组合.其中继承我们大家应该都比较熟悉,因为是C++的三大特性继承Inheritance,封装Encapsulation,和多态Poly ...

  3. UML类关系(依赖,关联,聚合,组合,泛化,实现)

    转自https://blog.csdn.net/k346k346/article/details/59582926   在学习面向对象设计时,类关系涉及依赖.关联.聚合.组合和泛化这五种关系,耦合度依 ...

  4. UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)

    UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父 ...

  5. java--依赖、关联、聚合和组合之间区别的理解

    在学习面向对象设计对象关系时,依赖.关联.聚合和组合这四种关系之间区别比较容易混淆.特别是后三种,仅仅是在语义上有所区别,所谓语义就是指上下文环境.特定情景等. 依赖(Dependency)关系是类与 ...

  6. java_UML:继承/泛化、实现、依赖、关联、聚合、组合的联系与区别 (2016-07-12)

    分别介绍这几种关系: UML关系:继承(泛化).实现.依赖.关联.聚合.组合的联系与区别 一.表示符号上的区别 二.具体区别与联系 1. 继承/泛化(Generalization) [泛化关系]:是一 ...

  7. UML[1] UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)(转)

    转自:http://blog.csdn.net/zhaoxu0312/article/details/7212152 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的是 ...

  8. UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现

    UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统中, ...

  9. UML类图及依赖,泛化,关联,聚合,组合,实现

    UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统中, ...

随机推荐

  1. HTTP与HTTPS有什么区别?

    HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全,为了保证这些隐私数据能加密传输,于是网景公司设计了SSL(Secure Sockets Layer)协议用 ...

  2. vue-router scrollBehavior无效的问题及解决方案

    在使用vue做单页面应用开发时候 使用vue-router作为路由控制器  在使用过程中发现每个页面打开都在原来的位置 不能返回到页面顶部位置 ,然后查看api文档 滚动行为  发现如下代码: con ...

  3. BZOJ4199/UOJ131 [Noi2015]品酒大会

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. Windows 安装Mysql8.0 绿色包

    〇.准备: MySQL8.0 Windows zip包下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip 环 ...

  5. 《Think in Java》(九)接口

    接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法: 抽象化类则是普通类与接口之间的一种中庸之道: 涨姿势了 接口也可以拥有值属性,但它们都是隐式的 static 和 final 的: 接 ...

  6. FreeDOS 实模式 保护模式

    FreeDOS可以运行在实模式或保护模式下,在启动FreeDOS时有4种运行模式选择: 前两种运行在保护模式下, 后两种运行在实模式下. 根据How to tell whether your CPU ...

  7. DH04-开放封闭原则

    模式简介 定义:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 无论模块是多么封闭,都会存在一些无法对之封闭的变化.对设计的模块预估可能发生变化种类,然后构造抽象来隔离变化. 解决:创建抽象 ...

  8. Eclipse插件开发_异常_01_java.lang.RuntimeException: No application id has been found.

    一.异常现象 在运行RCP程序时,出现 java.lang.RuntimeException: No application id has been found. at org.eclipse.equ ...

  9. 《Effective C++》——条款17:以独立语句将newed对象置入智能指针

    假设有如下两个函数: int priority(); void processWidget(std::tr1::shared_ptr<Widget>pw, int priority); 对 ...

  10. sass进阶篇

    @if @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块.在 Sass 中除了 @if 之,还可以配合 @ ...