Bidirectional and Unidirectional UML Associations

Although I think I understand aggregation and composition , I have difficulty understanding the association of bi-directional and uni-directional . I read that with bi-directional associations, both classes know about each other and with the help of uni-directional associations, only one of the relationship classes is known. However, this explanation seems too abstract for me, and I would like to know what this means especially for my code and for the program I am writing. It would be very nice if, along with the explanation, you could provide a simple example of how to translate these two into code (I prefer C ++, but it can be anything, including pseudocode)

+8
uml
source share
2 answers

Bidirectional associations are navigable from both ends. For example, given the following classes (for simplicity, suppose the association is 0..1 at both ends)

 class Parent { Child* children; } class Child { Parent* parent; } 

you can go from Parent to his child and vice versa: the parent knows about his child, the child knows about his parent and (if this.parent!=null ) this.parent.child==this (otherwise he will not be the same associations )

 Parent <---------> Child 

However, if the pointer to Parent in Child not a pointer:

 class Child { } 

it will be a one-way communication: you can switch from parent to child, but you cannot return from children to parent.

 Parent ----------> Child 
+20
source share

Unfortunately, the UML specification does not define the concept of “bidirectional association”, but only mentions “bidirectional navigation”, where “navigational ability” (indicated by an arrow in the class diagram) does not have a clearly defined computational value as it can be provided, for example, by reference properties or on request. The most effective form of “navigation” provided by reference properties is modeled by the UML concept of the association’s final property (indicated by a dot in the class diagram).

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

  • for this reference to the child, this.parent.child == this , as explained in a somewhat incomplete answer by Javier, but furthermore

  • for this parent reference, this.child.parent == this .

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

+1
source share

All Articles