关联是否暗示 UML 中的依赖关系? [英] Does an association imply a dependency in UML?
问题描述
在讨论我的回答时对此问题,关于如何对此代码建模存在一些分歧:
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();
}
}
关键点是:
Something
类是MainClass
中的一个属性,表示关联Something
类在MainClass
中被引用,表明存在依赖关系- 依赖应该是一个专门的关联
- the
Something
class is an attribute inMainClass
, suggesting an association - the
Something
class is referenced withinMainClass
, suggesting a dependency - A dependency is supposed to be a specialised association
但是,由于在供应商类不是属性的情况下,依赖项可能是合适的,因此使用依赖项确实隐藏"了 Something
是一个属性的意图,而不是简单地引用?
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?
推荐答案
依赖项和关联是两个不同的概念.根据UML元模型,两者都是关系"元类的两个独立子类.
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.
这篇关于关联是否暗示 UML 中的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!