组成和关联关系有什么区别? [英] What is the difference between Composition and Association relationship?

查看:141
本文介绍了组成和关联关系有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OOP中,类之间的组成关系(在UML中用实心菱形表示)和关联(在UML中用空菱形表示)之间的区别是什么?我有点困惑.什么是聚合?我可以举一个令人信服的真实示例吗?

In OOP, what is the difference between composition (denoted by filled diamond in UML) and association (denoted by empty diamond in UML) relationship between classes. I'm a bit confused. What is aggregation? Can I have a convincing real world example?

推荐答案

组成

想象一个由不同业务部门(或部门)组成的软件公司,例如存储业务部,网络业务部.汽车事业部这些业务部门的生命周期由组织的生命周期决定.换句话说,如果没有公司,这些业务部门就无法独立存在.这是组成. (即公司由业务部门组成)

Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. This is COMPOSITION. (ie the firm is COMPOSED OF business units)

关联

该软件公司可能有外部餐饮服务商,为员工提供食物.这些承办酒席不属于本公司.但是,他们与该公司相关联.即使关闭我们的软件公司,餐饮服务商也可以存在.他们可能会为另一家公司服务!因此,餐饮业者的生存期不受软件公司生存期的支配.这是典型的协会

The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm. This is typical ASSOCIATION

AGGREGATION

考虑一个汽车制造单位.我们可以将Car视为一个整体,将Car Wheel视为Car的一部分. (在这一点上,它看起来像是合成物...............................在此示例中,Wheel类的实例显然独立于Car类的实例而生存. 因此,与合成不同,在聚合中,所涉及对象的生命周期并不是紧密耦合的.

Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled.

这篇关于组成和关联关系有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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