nus-cs2113-AY2324S1 / forum

0 stars 0 forks source link

Unclear about navigability for class diagrams #38

Closed DextheChik3n closed 1 year ago

DextheChik3n commented 1 year ago

https://nus-cs2113-ay2324s1.github.io/website/schedule/week8/topics.html#design-modelling-modelling-structure-class-diagrams-basic

under the section OOP: Associations → Navigability, about the note on "two unidirectional associations in opposite directions do not add up to a single bidirectional association". I don't fully understand the example of the two unidirectional associations.

Does it mean that because the breeder Person object will always be different in relation to the pet Cat object, it is not a bidirectional association?

image

skylee03 commented 1 year ago

The breeder of the cat might be the same person keeping the cat, but might also be another person. If it is the former case, there are two association with opposite directions between the two objects in the object diagram, but in the class diagram, since this situation does not necessarily happen, we cannot treat it as a bidirectional association.

skylee03 commented 1 year ago

In other words, class diagrams must be compatible with all possible situations.

DextheChik3n commented 1 year ago

If it is the former case, there are two association with opposite directions between the two objects in the object diagram, but in the class diagram, since this situation does not necessarily happen, we cannot treat it as a bidirectional association.

I still feel confused by this statement you have mentioned as the textbook mentioned the former case (pet and owner) the class association is bidirectional

skylee03 commented 1 year ago

If it is the former case, there are two association with opposite directions between the two objects in the object diagram, but in the class diagram, since this situation does not necessarily happen, we cannot treat it as a bidirectional association.

I still feel confused by this statement you have mentioned as the textbook mentioned the former case (pet and owner) the class association is bidirectional

I guess the author of the textbook wants to use the semantics in natural language to help us understand the concept of "bidirectional association". In linguistics, converses are defined as "words which describe a relation between two entities from alternate viewpoints". For example, "pet" and "owner" can be regarded as a pair of converses, while "pet" and "breeder" cannot.

In fact, we are using UML to specify our programs, rather than drawing UML from the program code. The two pieces of code in your screenshot are completely equivalent. They do not place any restrictions on the referenced objects.

DextheChik3n commented 1 year ago

I guess the author of the textbook wants to use the semantics in natural language to help us understand the concept of "bidirectional association". In linguistics, converses are defined as "words which describe a relation between two entities from alternate viewpoints". For example, "pet" and "owner" can be regarded as a pair of converses, while "pet" and "breeder" cannot.

I understand the purpose of using the entity names of the respective pairs but I still feel that my initial question has not been answered... I think the main thing I am confused about is when do we use a bidirectional arrow and when do we use two unidirectional arrows?

skylee03 commented 1 year ago

I guess the author of the textbook wants to use the semantics in natural language to help us understand the concept of "bidirectional association". In linguistics, converses are defined as "words which describe a relation between two entities from alternate viewpoints". For example, "pet" and "owner" can be regarded as a pair of converses, while "pet" and "breeder" cannot.

I understand the purpose of using the entity names of the respective pairs but I still feel that my initial question has not been answered... I think the main thing I am confused about is when do we use a bidirectional arrow and when do we use two unidirectional arrows?

class A {
    B b;
    //...
}

class B {
    A a;
    //...
}

A a;
B b;

Bidirectional arrow: if the references are not null, then "a.b.a == a and b.a.b == b" must hold. Two unidirectional arrows: if the references are not null, then "a.b.a == a and b.a.b == b" may not hold.

okkhoy commented 1 year ago

I guess the author of the textbook wants to use the semantics in natural language to help us understand the concept of "bidirectional association". In linguistics, converses are defined as "words which describe a relation between two entities from alternate viewpoints". For example, "pet" and "owner" can be regarded as a pair of converses, while "pet" and "breeder" cannot.

I understand the purpose of using the entity names of the respective pairs but I still feel that my initial question has not been answered... I think the main thing I am confused about is when do we use a bidirectional arrow and when do we use two unidirectional arrows?

When you know the two variables in 2 classes refer to the same association use bidirectional association. At runtime, the objects are associated with each other. When you know the two variables in 2 classes can refer to different set of objects at run time, use 2 unidirectional associations.

When you look a the code (particularly with variable names a, b, c etc.,) I is hard to say which is which.

You need to context/information of the problem you are modeling.

E.g., Car and Person, if the context is you are modeling ownership, then it could be bidirectional (person owns the car, car belongs to the person); if it is some other application where you are modeling person drives some car and car is driven by some person then it is 2 unidirectional associations.

DextheChik3n commented 1 year ago

oooh ok I get it now, thank you prof Akshay and Ming Tian for the help! 🙏