双向和单向关联 UML [英] bi-directional and uni-directional associations UML
问题描述
虽然我认为我了解 aggregation
和 composition
,但我很难理解 bi-directional
和 uni-directional代码>关联.我已经阅读了
bi-directional
关联,两个类都知道彼此和 uni-directional
关联只有一个类知道的关系.然而,这个解释对我来说似乎太抽象了,我会想知道这对我的代码和我正在编写的程序特别意味着什么.如果你能连同解释一起提供一个简单的例子,那就太好了这两个如何转换为代码(我更喜欢 c++,但它可以是任何东西,包括伪代码)
While I think I understand aggregation
and composition
, I am having difficulties understanding bi-directional
and uni-directional
association.
I've read that with bi-directional
association, both classes know about
each other and with uni-directional
association only one of the classes is aware
of the relationship. However this explanation seems too abstract for me and I would
like to know what this in particular means for my code and for the program I am writing.
It would be very nice if you could, along with explanation, provide a simple example
of how these two translate to code (I prefer c++, but it can be anything including pseudocode)
推荐答案
双向关联可从两端进行导航.例如,给定以下类(为简单起见,假设两端的关联为 0..1)
Bi-directional associations are navigable from both ends. For instance, given the following classes (for simplicity, suppose that the association is 0..1 in both ends)
class Parent {
Child* children;
}
class Child {
Parent* parent;
}
您可以从 Parent
转到其子项,反之亦然:父项知道其子项,子项知道其父项,并且(如果 this.parent!=null
) this.parent.child==this
(否则不会是同一个关联).
you can go from a Parent
to its child, and vice-versa: the parent knows about its child, the child knows about its parent, and (if this.parent!=null
) this.parent.child==this
(othewise it would not be the same association).
Parent <---------> Child
但是,如果Child
中没有指向Parent
的指针:
However, if there were no pointer to Parent
in Child
:
class Child { }
这将是一个单向关联:你可以从父到子,但不能从子到父.
it would be an uni-directional association: you can go from parent to child, but you cannot go back from children to parent.
Parent ----------> Child
这篇关于双向和单向关联 UML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!