2

After reading the question "In UML class diagram can composition be bidirectional?", I'm wondering how concrete examples of bi-directional shared/composite aggregation will look on a UML diagram. Specifically, I'm interested how arrow-heads and multiplicity are used to distinguish between the following cases:

1) Uni-directional shared aggregation (aggregation)

class Foo {
    // Can contain 0+ elements
    ArrayList<Bar> bars = new ArrayList<>();
    
    void addBar(Bar bar) {
        bars.add(bar);
    }
}

class Bar {...}

2) Bi-directional shared aggregation (aggregation)

class Foo {
    // Can contain 0+ elements
    ArrayList<Bar> bars = new ArrayList<>();
    
    void addBar(Bar bar) {
        bars.add(bar);
        bar.setFoo(this);
    }
}

class Bar {
    Foo foo;
    
    void setFoo(Foo foo) {
        this.foo = foo;
    }
}

3) Uni-directional composite aggregation (composition)

class Foo {
    // Can contain 0+ elements
    ArrayList<Bar> bars = new ArrayList<>();

    void addBar() {
        bars.add(new Bar());
    }
}

class Bar {...}

4) Bi-directional composite aggregation (composition)

class Foo {
    // Can contain 0+ elements
    ArrayList<Bar> bars = new ArrayList<>();

    void addBar() {
        bars.add(new Bar(this));
    }
}

class Bar {
    Bar foo;
    
    Bar(Foo foo) {
        this.foo = foo;
    }
}

Case 1 and 2 should be straightforward. I'm not sure how to represent the bi-directional associations, and how to distinguish them from the uni-directional ones, as both will have multiplicities on both sides. The diagram must include multiplicities.

aiwl
  • 137
  • 1
  • 6
  • 1
    Please read the term definition on p. 110 of UML 2.5. It's shared/composite aggreation. Not aggregation/composition. – qwerty_so Oct 02 '21 at 18:21
  • I have found a similar question here: https://stackoverflow.com/q/47855657/14280715 – aiwl Oct 03 '21 at 04:42
  • @qwerty_so Thanks, I edited the question to use the correct terminology. – aiwl Oct 03 '21 at 04:52

1 Answers1

1

Arrowheads are used to distinguish uni-directional from bi-directional associations.

  • An uni-directional association has one arrowhead.
  • A bi-directional association has two arrowheads, one on each side.

Normally, an association without arrowheads has an unspecified navigability. But it is allowed to have the convention that such assocations are always bi-directional.

To explicitly indicate that an association is not navigable in a certain direction, you may display a cross at the non-navigable end, but this is not used very often in practice.

Multiplicities and the aggregation type are not relevant for the navigability.

For example, your last case can be modeled as follows:

bidirectional-composition

For more examples, see UML 2.5 specification, section 11.5.5.

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • 1
    I would rather think that non-navigability is the only useful one (though as you mentioned of rare use). The OMG guys more or less abandoned the navigability in the latest UML 2.5 spec and introduced the `isOwned` property (dot notation). – qwerty_so Oct 02 '21 at 20:52
  • Thank you, based on your answer I was able to find a similar SO question: https://stackoverflow.com/q/47855657/14280715 – aiwl Oct 03 '21 at 04:41
  • @qwerty_so Based on your comment I was able to find an answer discussing the use of ownership vs. navigability: https://stackoverflow.com/a/45596200/14280715 – aiwl Oct 03 '21 at 04:48