In discussion about my answer to this question, there was some disagreement over how to model this code:
public class MainClass
{
private Something something;
public void Action()
{
OtherClass other = something.GetOtherClass();
}
}
The key points being:
- the
Somethingclass is an attribute inMainClass, suggesting an association - the
Somethingclass is referenced withinMainClass, suggesting a dependency - A dependency is supposed to be a specialised association
However, since a dependency is can be appropriate in cases where the supplier class is not an attribute, does using a dependency “hide” the intention that the Something is an attribute, rather than simply referenced?
Furthermore, does an association, which represents an attribute in a class, imply a dependency because it is being stored (and presumably referenced and used in some way).
So, with reference to the above points, does an association imply a dependency, and how would you model the above code in a class diagram?
Dependencies and associations are two different concepts. According to the UML metamodel, both are two independent subclasses of the “Relationship” metaclass.
However, it is true, that in your scenario I’d just model an association between the two classes and not a dependency. The fact that the two classes are connected through an association already make them dependent.