bi-directional and uni-directional associations UML
associations, uml
Solution
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
Problem
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)