nus-cs2103-AY2021S1 / forum

20 stars 2 forks source link

Association vs Dependency #486

Closed junlong4321 closed 3 years ago

junlong4321 commented 3 years ago

Q1 Follow up of #480

In issue 480, it was mentioned that "A parameter may not necessarily indicate an association but it certainly indicates a dependency."

However, in the textbook, an association is defined to be an interaction between two objects. That being said, having a parameter of one object in a class of another could potentially indicate an interaction between two objects. image

As such, can I clarify what exactly constitutes an association and a dependency? Can we see an association as a strong dependency since it was mentioned in issue 480 that "An easier way to remember is, the solid arrow already contains a dashed arrow inside it."? If so, where do we draw the line between weak and strong dependencies?

Q2 Also, can I clarify if high cohesion always implies low coupling? My reason for asking is because a class could be very focused on its individual responsibility, but its responsibility may involve the usage of other classes.

damithc commented 3 years ago

As such, can I clarify what exactly constitutes an association and a dependency? Can we see an association as a strong dependency since it was mentioned in issue 480 that "An easier way to remember is, the solid arrow already contains a dashed arrow inside it."? If so, where do we draw the line between weak and strong dependencies?

An association in a UML diagram is represented in the code as an instance-level variable. If there is no such connection between the two classes but one still depends on the other (e.g., by way of receiving an object of the other as a parameter), that can be shown as a dependency in a UML diagram.

Also, can I clarify if high cohesion always implies low coupling? My reason for asking is because a class could be very focused on its individual responsibility, but its responsibility may involve the usage of other classes.

As you realized, not always. When one increases the cohesion, the nature of coupling can change along the way. Some couplings may disappear while some new ones will have to be added. One shouldn't think of coupling as an absolute quantity; for example, two 'expected' couplings may be better than one 'unexpected/unnatural' coupling.