3

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:

  1. the Something class is an attribute in MainClass, suggesting an association
  2. the Something class is referenced within MainClass, suggesting a dependency
  3. 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?

Community
  • 1
  • 1
chrisbunney
  • 5,819
  • 7
  • 48
  • 67

3 Answers3

4

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.

Jordi Cabot
  • 8,058
  • 2
  • 33
  • 39
  • +1 for pointing out I misunderstood the relationship between associations and dependencies. Confirmed it by digging through the UML Superstructure specification from http://www.omg.org/spec/UML/2.2/ – chrisbunney Jul 20 '09 at 11:17
  • Left open to see if there were any more answers, since there haven't been any and I'm satisfied this answer addresses the question, I've accepted it – chrisbunney Dec 13 '10 at 11:55
0

There are three class, the first is MainClass, the second is Something, and the third is OtherClass. It is so simple if you use UML tools to generate the class you will see the diagrams. It is compose of thee class. I believe the dash line connected between MainClass and OtherClass. The line(association) connected between MainClass and Something.

Piotr Sobiegraj
  • 1,775
  • 16
  • 26
0

I would argue it really depends what you are trying show with the particular diagram. You could model it with either. You could even use composition if it made sense in the context.

Mike G
  • 746
  • 5
  • 19