UML类与其在创建另一个对象时所创建的对象之间的关系?

huangapple go评论66阅读模式
英文:

UML relationship of class and object it creates inside the creation of another object?

问题

例如,在Java中:

public class App {

    public void method() {

        Object1 o1 = new Object1(new Object2(parameters));
    }

}

我知道App和Object1之间有一个组合关系。

但是App和Object2呢?它也是一个组合关系吗?

英文:

For example, in java:

public class App {

    public void method() {

        Object1 o1 = new Object1(new Object2(parameters));
    }

}

I know App and Object1 have a composition relationship.

But what about App and Object2? Is it a composition as well?

答案1

得分: 2

在方法中使用类不足以形成关联

你的类 App 没有包含类 Object1Object2 的字段。它只是在方法的实现中使用了 Object1Object2。这对于建立关联是不足够的:AppObjectX 之间没有概念上的关系,这只是实现的细节。由于组合是一种特殊类型的关联,所以这里也没有组合关系。

使用类意味着依赖关系

由于你的 App 使用了 Object1Object2,存在一个 «use» 依赖关系App 需要了解这些类。你可以用一个带有开放箭头的虚线箭头表示这种依赖关系。

然而,你的示例中的依赖仅限于方法实现的级别,而不涉及类本身的级别。你可以用其他方式实现该方法。因此,我建议在你的模型中不要显示这种易变的依赖关系。只有在类定义本身会使用这样的对象时(例如,如果一个方法会返回一个 ObjectX 或使用一个 ObjectX 参数),才建议显示这种依赖关系。

术语:并非所有的组合都是组合!

如前所述,这里没有组合关系。尽管如此,这个词是有歧义的:

  • 它可能指的是对象组合。这只涉及到对象包含另一个类的字段。

  • 它可能指的是UML 组合。这是一种具有独占所有权的特殊类型的关联。

英文:

Using a class in a method is not sufficient for an association

Your class App has no fields of class Object1 or Object2. It just uses Object1 and Object2 in the implementation of a method. This is not sufficient for making an association: there is no conceptual relationship between App and ObjectX; it's just an implementation detail. And since composition is a special kind of association, there is no composition either.

Using a class means a dependency

Since your App uses Object1 and Object2, there is a «use» dependency: App needs to know these classes. You could show this dependency with an open headed dashed arrow.

However, the dependency in your example is only at the level of the method implementation and not at the level of the class itself. You could implement the method otherwise. I'd therefore advise not to show such a volatile dependency in your model. The dependency would be advisable if the class definition itself would use such an object (e.g. if a method would return an ObjectX or use an ObjectX parameter).

Terminology: Not all compositions are compositions!

As explained, there is no composition here. Nevertheless, the word is ambiguous:

  • it could mean object compostion. This is just about objects having fields of another class.

  • it could mean UML composition. This is a special kind of association with exclusive ownership

huangapple
  • 本文由 发表于 2020年10月1日 09:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64147816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定