Does an association imply a dependency in UML? [cl

2019-01-26 20:49发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.