类图中的组合和聚合
在设计类图时,我很难理解这些,据我所知,组合是一种“有一个”关系,但聚合?我不知道那是什么。还有一件事我什么时候才能知道在类图中使用组合?我什么时候才能知道在类图中使用聚合?一个例子将受到高度赞赏。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在设计类图时,我很难理解这些,据我所知,组合是一种“有一个”关系,但聚合?我不知道那是什么。还有一件事我什么时候才能知道在类图中使用组合?我什么时候才能知道在类图中使用聚合?一个例子将受到高度赞赏。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
聚合和组合之间的主要区别在于“has-a”关系的属性。它要么强,要么弱。
聚合“has-a”关系是“弱类型”。弱意味着聚合器的链接组件可能会在聚合生命周期中存活下来,或者可以通过其他方式访问。一个简单的例子是一个足球俱乐部及其成员。如果俱乐部解散,你仍然拥有会员 - 事实上,这些会员也可能是其他俱乐部的会员,因此仍然存在。
组合“has-a”关系是“强类型”的。强烈的意思是一个人不能离开另一个人而存在。组件的生命周期直接链接到“父组件”。一个例子是有房间的房子。如果你决定拆掉房子,你也将失去你的房间。
也许有点抽象,但我认为这就是其背后的想法。
The main difference between an aggregation and a composition is the property of your "has-a" relationship. It' either strong or weak.
The aggregations "has-a" relationship is of "weak-type". Weak meaning the linked components of the aggregator may survive the aggregations life-cycle or may be accessed in some other way. A simple example would be a football club with its members. If the club is dissolved, you still got the members - which in fact could also be members of other clubs and thus are kept alive.
The composition "has-a" relationship is of "strong-type". Strong meaning that one can't exist without the other. The component's life-cycles are directly linked to the "parent". An example would be a house with rooms. If you decide to tear the house down, you will also lose your rooms.
Maybe a little abstract but I think that's the idea behind it.
此处查看之前问题的答案,<一个href="https://stackoverflow.com/questions/7195151/conversion-of-association-aggregation-and-composition-into-code-in-java/7197390#7197390">此处 和 此处。
我个人不使用聚合。语义太弱而无用。它造成的问题多于它解决的问题。只有一处具有明确定义且可能有用的属性,将其与简单的二元关联区分开来(请参阅脚注)。
组合可能很有用,因为它定义了有关不变性和生命周期管理的重要属性(请参阅上面的第一个链接)。我已经很幸福地生存了很多年,不需要使用聚合。
嗯。
脚注。如果您需要对递归树关系进行建模,例如将部分递归分解为子部分,则可以使用聚合。聚合表示不能存在循环关系,即一个部分不能直接或间接地成为其自身的子部分。然而,大多数阅读该模型的人不太可能知道这一点。 -所以你需要添加评论。这意味着您最好坚持使用二元关联并避免使用聚合带来的混乱。
See answers to previous questions here, here and here.
Personally I don't use Aggregation. The semantics are too weak to be useful. It causes more problems than it solves. There's only one place where it has well-defined and potentially useful properties that distinguish it from a simple binary association (see footnote).
Composition can be useful because it defines important properties on immutability and lifecycle management (see 1st link above). I've survived quite happily for many years without ever needing to use Aggregation.
hth.
Footnote. Aggregation can be applicable if you need to model a recursive tree relationship, e.g. a part decomposed recursively into sub-parts. Aggregation says there can be no circular relationships, i.e. a part can't be a sub-part of itself - directly or indirectly. However most people reading the model are unlikely to know that. -So you'd need to add a comment. Which means you'd be as well to stick with a binary association and avoid the confusion of using Aggregation.