概要

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/

最新文章

  1. Linux命令详解之—less命令
  2. spring boot Swagger 集成
  3. 基于Extjs的web表单设计器 第四节——控件拖放
  4. delphi中的ClientDataSet组件的open和Execute方法各自在什么情况下用?
  5. 【NOIP 2014 DAY1 T3】飞扬的小鸟(DP)
  6. Javascript 常用函数【3】
  7. Div在BOdy中居中
  8. (转)DataTable添加行出现“该行已经属于另一个表”的错误!
  9. 渐进式Web应用(PWA)入门教程(上)
  10. 1068: [SCOI2007]压缩
  11. java控制多线程同时写一个文件
  12. MySql思维导图
  13. one-hot编码
  14. [VUE ERROR] Invalid default value for prop "slides": Props with type Object/Array must use a factory function to return the default value
  15. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
  16. 《Linux 性能及调优指南》3.1 确认瓶颈
  17. BZOJ1088或洛谷2327 [SCOI2005]扫雷
  18. BP神经网络原理详解
  19. JavaScript数据结构-3.List
  20. 【数据库】10.0 MySQL常用语句(一)

热门文章

  1. python去掉行尾的换行符
  2. ios 发布相关材料记录
  3. 重定向【TLCL】
  4. 文件系统中跳转【TLCL】
  5. ajax01简介
  6. div css 练习2
  7. 免配置环境变量使用Tomcat+设置项目主页路径为http://localhost:8080+修改tomcat端口号
  8. Android Studio2.3中简单配置,释放C盘空间
  9. C#二进制与字符串互转换,十六进制转换为字符串、float、int
  10. 数据链路层--PPP协议