简体   繁体   English

在UML class图中,组合可以是双向的吗?

[英]In UML class diagram can composition be bidirectional?

Can composition be bidirectional in a way that both classes are aware of each other?组合能否以两个类都知道彼此的方式是双向的?

And if not, what is the default direction of composition?如果不是,默认的构图方向是什么?

You should distinguish navigability and aggregation. 您应该区分可导航性和聚合。 Arrow and diamond. 箭和钻石。

Arrow A->B means only that B is reachable from A in some simple way. 箭头A-> B表示只能以某种简单的方式从A到达B. If A contains a composition of B, it means that 如果A包含B的组成,则意味着

the composite object has responsibility for the existence and storage of the composed objects (parts). 复合对象负责组合对象(部件)的存在和存储。

(citation from OMG Unified Modeling Language TM (OMG UML) - p.109) (引自OMG Unified Modeling Language TM(OMG UML) - p.109)

So, can congregation have bi-directional navigability? 那么,会众可以双向导航吗?

Yes. 是。 It is quite normal. 这很正常。

If, for example, you have decided to destroy B in some of its functions, you MUST reach A and destroy it from there. 例如,如果您决定在其某些功能中销毁B,则必须到达A并从那里销毁它。 So, composition has bi-directional navigability often enough. 因此,组合具有足够的双向导航性。 Notice, that bi-directional navigability, according to both current and coming UML standards, is shown as line without arrows on both sides. 请注意,根据当前和未来的UML标准,双向导航性显示为两侧没有箭头的线。 Both-sided arrow is deprecated. 双面箭头已弃用。 THAT is the reason you won't see it often. 这就是你不经常看到它的原因。

Can the composition itself be bi-directional? 组合物本身可以是双向的吗? Can we see black diamonds on both sides of an association? 我们可以在协会的两边看到黑钻吗?

No , of course this sort of association cannot be mutual, for it is impossible for B to be created in A only and, simultaneously, for A to be created in B only. ,当然这种关联不可能是相互关联的,因为B不可能仅在A中创建,同时,A只能在B中创建。

What is interesting, the shared aggregation (empty diamond) cannot be mutual, too, but here the limitation is not inherent, it is simply forbidden by UML standard. 有趣的是, shared aggregation (空钻石)也不可能是相互的,但这里的限制并不是固有的,它只是被UML标准所禁止。

Yes, Composition does not add constraints with regards to the navigability of the association. 是的,Composition不会添加关于协会可导航性的约束。 More info on the difference between Accociation, Composition and Aggregations can be found here: UML Composition vs Aggregation vs Association 关于Accociation,Composition和Aggregations之间区别的更多信息可以在这里找到: UML组合与聚合与关联

From https://www.lucidchart.com/pages/uml/class-diagram : 来自https://www.lucidchart.com/pages/uml/class-diagram

Bidirectional associations are the default associations between two classes and are represented by a straight line between two classes. 双向关联是两个类之间的默认关联,由两个类之间的直线表示。 Both classes are aware of each other and of their relationship with each other. 两个班级都相互了解彼此之间的关系。 In the example above, the Car class and RoadTrip class are interrelated. 在上面的示例中,Car类和RoadTrip类是相互关联的。 At one end of the line the Car takes on the association of "assignedCar" with the multiplicity value of 0..1 which means that when the instance of RoadTrip exists, it can either have one instance of Car associated with it or no Cars associated with it. 在该行的一端,Car采用“assignedCar”与多重性值0..1的关联,这意味着当RoadTrip的实例存在时,它可以有一个与之关联的Car实例,或者没有关联的Cars用它。 In this case, a separate Caravan class with a multiplicity value of 0..* is needed to demonstrate that a RoadTrip could have multiple instances of Cars associated with it. 在这种情况下,需要一个多重值为0 .. *的单独的Caravan类来证明RoadTrip可能有多个与之关联的Cars实例。 Since one Car instance could have multiple "getRoadTrip" associations-- in other words, one car could go on multiple road trips--the multiplicity value is set to 0..* 由于一个Car实例可以有多个“getRoadTrip”关联 - 换句话说,一辆汽车可以进行多次公路旅行 - 多重性值设置为0 .. *

In the past I had the same opinion as Gangnus with过去我和 Gangnus 有同样的看法

So, can composition have bi-directional navigability?那么,组合可以具有双向导航性吗?

But following some recent discussion I had a more detailed look into the UML specs.但是在最近的一些讨论之后,我对 UML 规范有了更详细的了解。 And simply, that statement is not true (only partially).简而言之,该陈述是不正确的(仅部分正确)。 Let's look into the UML 2.5 specs.让我们看看 UML 2.5 规范。 On p.在第110 it is stated 110据说

Sometimes a Property is used to model circumstances in which one instance is used to group together a set of instances;有时 Property 用于 model 情况,其中一个实例用于将一组实例组合在一起; this is called aggregation.这称为聚合。 To represent such circumstances, a Property has an aggregation property, of type AggregationKind;为了表示这种情况,Property 具有 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: AggregationKind 是具有以下文字值的枚举:

[omitting shared aggregation] [省略共享聚合]

composite: Indicates that the Property is aggregated compositely, ie, the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3). composite:表示该Property是组合聚合的,即组合object负责组合对象的存在和存储(见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.复合聚合是一种强聚合形式,它要求一次最多将一个部分object包含在一个复合object中。 If a composite object is deleted, all of its part instances that are objects are deleted with it.如果删除组合object ,则其作为对象的所有部分实例都将被删除。

Note my emphasis on the object/instance in the above text.请注意我在上面文本中对对象/实例的强调。 So UML just talks of responsibility.所以UML只讲责任。 If A composes B it will be responsible to delete B when it is destroyed itself.如果A组成B ,它将负责在 B 被销毁时删除B Vice versa B would be responsible for A 's destruction.反之亦然, B将负责A的销毁。 So, if you have references in both directions (ie diamonds on both sides) then you will be reponsible to delete the object on the other side.因此,如果您在两个方向都有引用(即两边都是菱形),那么您将负责删除另一边的 object。 This of course works only if just one of both holds a reference to the other.这当然只有在两者之一持有对另一个的引用时才有效。 If both would have a reference, it would not be possible to have a directed responsibility (because it's circular).如果两者都有参考,就不可能有直接责任(因为它是循环的)。

I still think that having composite aggregation on both sides is not really a good idea.我仍然认为在两侧进行复合聚合并不是一个好主意。 But according to the specification it is possible.但根据规范,这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM