UML:一个 class 可以通过组合聚合成另外两个 class 吗?

UML: Can a class be aggregated through composition to two other classes?

想象一下这些 classes,其中 SeasonPoints 按组合聚合为 TeamSeason,其中 Team 在许多 [=14] 中播放=]s 可以有多个 SeasonPoints,一个 Season 有很多 Teams 可以有多个 SeasonPoints 来自不同的 Teams.

请注意,为了 SeasonPoints 存在,它需要一支得分的球队和得分的赛季,所以我看不出制作此图的其他方式:

       1        n             n       1
|Team|<comp>----|SeasonPoints|-----<comp>|Season|

问题激增,因为这样你可以摧毁 TeamSeasonSeasonPoints 仍然存在(在另一个未被摧毁的 class 中) ,这意味着容器可以在不破坏它包含的 class 的情况下被销毁,并且组合说如果容器被销毁,那么包含的 classes.

规格如下:

Sometimes a Property is used to model circumstances in which one instance is used to group together a set of instances; this is called aggregation. To represent such circumstances, a Property has an aggregation property, of type AggregationKind; the instance representing the whole group is classified by the owner of the Property, and the instances representing the grouped individuals are classified by the type of the Property. AggregationKind is an enumeration with the following literal values:

none: Indicates that the Property has no aggregation semantics.

shared: Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.

composite: Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3).

Composite aggregation is a strong form of aggregation that requires a part object be included in at most one composite object at a time. If a composite object is deleted, all of its part instances that are objects are deleted with it.

所以基本上你的模型是错误的(根据最后一句话)。

但是 - 最好忘记聚合,因为它们引入的模糊比它们实际添加的语义更多。关于这个有很多讨论。一辆汽车 车轮组成,甚至如上述规格在示例中所示?要知道,轮子可以自生自灭,不会在汽车被毁坏时“被毁坏”。当您为数据库建模时,这可能很有用(删除依赖信息)或当您必须为内存建模时(免费 space 用于复合元素)。但在几乎所有情况下,与多重性的简单关联就足够了。