bi-directional and uni-directional associations UML

无人久伴 提交于 2019-12-03 13:16:06

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;
}

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

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

Unfortunately, the UML specification does not define any concept of "bidirectional association", but only mentions "bidirectional navigability" where "navigability" (indicated with an arrow in a class diagram) does not have a well-defined computational meaning as it can be provided, e.g., by reference properties or by queries. The most efficient form of "navigability", provided by reference properties, is modeled with the UML concept of association end ownership (indicated with a dot in a class diagram).

Bidirectional associations can be precisely defined as a pair of mutually inverse reference properties. This definition implies two conditions:

  1. for this referencing a child, this.parent.child == this, as explained in the somewhat incomplete answer of Javier, but in addition also

  2. for this referencing a parent, this.child.parent == this.

You can read more about this, and find many class diagrams describing examples of bidirectional associations in the tutorials Managing Bidirectional Associations in Java EE and Managing Bidirectional Associations in JavaScript.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!