Dependencies vs. Associations

1 view
Skip to first unread message

Caroline

unread,
Apr 18, 2011, 6:42:19 PM4/18/11
to UBC CPSC 210
Is it safe to say that an association between two classes - typeA and
typeB - happens when an object of typeA is a class variable in typeB
and that a dependency happens when some method in typeA gets
information from typeB?

I'm still a little fuzzy on how to tell for sure.

Thanks!

Paul Carter

unread,
Apr 18, 2011, 9:25:53 PM4/18/11
to ubc-cp...@googlegroups.com
A uni-directional 1-to-1 association from classA to classB represents a relationship that exists for the lifetime of an instance of classA and is implemented by having a field of type classB as a member of classA. So, as soon as an instance of classA is created, this object has a field of type classB (although the value of that field might be null for some part of the lifetime of the object).

A dependency from classA to classB represents a more transient relationship. So, for example, classA could have a method that takes a parameter of type classB (so the dependency occurs only during a call to this method) or classA could have a method that has a local variable of type classB (so again the dependency occurs only during a call to this method). The dependency indicates that if we were to make a change to any member of classB to which classA has access, we may have to make a corresponding change to classA.

As I indicated in lecture, dependencies are not normally shown on UML class diagrams. We included them on some class diagrams to illustrate that our design led to dependencies on abstractions rather than on concrete types.

Paul

Reply all
Reply to author
Forward
0 new messages