聚合必须是一对多的吗? [英] Does aggregation have to be one-to-many?

查看:27
本文介绍了聚合必须是一对多的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直避免使用聚合,因为将一对多关系归类为聚合似乎太主观了.但我正在审查其他人制作的模型,其中聚合用于多对多关系(例如:一门课程由多个模块组成,一个模块可能是多个课程的一部分).这在我看来是完全错误的,但我找不到一个明确的规则来反对它.官方的裁决是什么?

I've always avoided using aggregation because it seems so subjective which one-to-many relationships should be classed as aggregations. But I'm reviewing a model produced by someone else in which aggregations are used for many-to-many relationships (as in: a course consists of several modules, a module may be part of several courses). That strikes me as plain wrong, but I can't find a definitive rule against it. What's the official ruling?

推荐答案

两件事:

  1. 是否允许共享聚合?根据 UML 规范,是的.
  2. 在实践中有用吗?通常我会说不.

我不喜欢 UML 聚合关系.虽然所有权在直觉上很有吸引力,但实际上它太主观了.我不使用它,并且通常不推荐使用它(尽管参见脚注).相反,请关注重要问题:

I am not a fan of the UML Aggregation relationship. Whilst ownership is intuitively appealing, it is too subjective practically. I don't use it, and generally don't recommend it be used (although see footnote). Instead, focus on the important questions:

  1. 什么是基数?
  2. 什么是创建/删除行为?
  3. 为什么存在这种关系?(即关系捕获的是什么业务事实/规则?

以上所有都可以通过直接关联来完成.如果答案是 (a) 它是一对多的,(b) 'one' 端负责创建/删除 'many' 端,并且 (c) 你真的想要,那么使用 Composite 关联.然而,聚合通常不会提高模型的可读性,它会增加混乱并有损于呈现底层域规则/要求.

All above can be done with straight associations. If the answer is (a) it's one to many, (b) the 'one' end is responsible for creating/deleting the 'many' end and (c) you really want to, then use the Composite association. Aggregation however doesn't generally improve readability of the model, it adds confusion and detracts from surfacing the underlying domain rules/requirements.

脚注:在一种场景中,聚合确实具有明确定义的语义并且可能很有用.具体来说,如果您有递归关系,聚合表示生成的对象结构是非循环的(即 DAG).缺点是相对很少有人意识到财产——当然不是业务领域的专家.所以你通常必须突出显示,例如在评论/约束中.

footnote: there is one scenario where Aggregation does have well-defined semantics and can be useful. Specifically, if you have a recursive relationship, Aggregation says the resultant object structure is acyclic (i.e. a DAG). Downside is relatively few people realise that property - certainly not business domain experts. So you typically have to highlight anyway, e.g. in a comment / constraint.

这篇关于聚合必须是一对多的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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