nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

What does it mean "Bidirectional associations require matching variables in both classes". #386

Closed adam07018 closed 1 year ago

adam07018 commented 1 year ago

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?

hingen commented 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 chairs and 4 players. Hence, there must be a player who is not associated with a chair but all chairs 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".

adam07018 commented 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 chairs and 4 players. Hence, there must be a player who is not associated with a chair but all chairs 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".

Thanks for your explanation. Clear example.

damithc commented 1 year ago

Good answer @hingen

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.

... and, if the first object's variable refers to the second object, the second object's variable should refer to the first object.