Closed adam07018 closed 1 year ago
I think it is possible to have a class foo
be in a bi-directional relationship with a class bar
but both of them have different multiplicities.
Let's show this in an example. In a game of musical chairs, we have chair
and player
. If a chair
and a player
have a bi-directional relationship, it means the player
is sitting on the chair
. Now suppose there are 3 chair
s and 4 player
s. Hence, there must be a player
who is not associated with a chair
but all chair
s are associated with a player.
Hence player
has an optional association with chair
(0..1 multiplicity). chair
has a compulsory association with player
(1 multiplicity).
Bidirectional associations require matching variables in both classes
Source of this statement is here
From my understanding, this statement is with regards to implementing a bidirectional relationship. It's just saying that if foo
and bar
are in a bi-directional relationship, then foo
must contain a variable of type bar
and bar
must contain a variable of type foo
. Don't think it was making any comment with regards to multiplicity, especially since right after that section it is mentioned that "To implement other multiplicities, choose a suitable data structure".
I think it is possible to have a class
foo
be in a bi-directional relationship with a classbar
but both of them have different multiplicities.Let's show this in an example. In a game of musical chairs, we have
chair
andplayer
. If achair
and aplayer
have a bi-directional relationship, it means theplayer
is sitting on thechair
. Now suppose there are 3chair
s and 4player
s. Hence, there must be aplayer
who is not associated with achair
but allchair
s are associated with a player.Hence
player
has an optional association withchair
(0..1 multiplicity).chair
has a compulsory association withplayer
(1 multiplicity).Bidirectional associations require matching variables in both classes
Source of this statement is here
From my understanding, this statement is with regards to implementing a bidirectional relationship. It's just saying that if
foo
andbar
are in a bi-directional relationship, thenfoo
must contain a variable of typebar
andbar
must contain a variable of typefoo
. Don't think it was making any comment with regards to multiplicity, especially since right after that section it is mentioned that "To implement other multiplicities, choose a suitable data structure".
Thanks for your explanation. Clear example.
Good answer @hingen
From my understanding, this statement is with regards to implementing a bidirectional relationship. It's just saying that if
foo
andbar
are in a bi-directional relationship, thenfoo
must contain a variable of typebar
andbar
must contain a variable of typefoo
.
... and, if the first object's variable refers to the second object, the second object's variable should refer to the first object.
For example, I have a class foo and another class bar which have bidirectional assocaitions pointing to each other.
Would I be allowed that foo have optional associations of bar (0..1 multiplicity) and bar have compulsory associations (1 multiplicity) ?
Or is it foo and bar must have same multiplicity toward each other?