它在创建另一个对象时创建的类和对象的UML关系? [英] UML relationship of class and object it creates inside the creation of another object?

查看:38
本文介绍了它在创建另一个对象时创建的类和对象的UML关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如在java中:

public class App {

    public void method() {

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

}

我知道 App 和 Object1 有组合关系.

I know App and Object1 have a composition relationship.

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

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

推荐答案

在方法中使用类不足以建立关联

您的App 类没有Object1Object2 类的字段.它只是在方法的实现中使用了 Object1Object2.这不足以建立关联:AppObjectX 之间没有概念上的关系;这只是一个实现细节.而且由于组合是一种特殊的联想,所以也不存在组合.

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.

因为您的 App 使用 Object1Object2,所以有一个 «use» dependency:App 需要知道这些类.你可以用一个开放的虚线箭头来表示这种依赖关系.

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.

但是,您示例中的依赖项仅在方法实现级别,而不是在类本身级别.否则,您可以实现该方法.因此,我建议不要在您的模型中显示这种不稳定的依赖关系.如果类定义本身将使用这样的对象(例如,如果一个方法将返回一个 ObjectX 或使用一个 ObjectX 参数),则该依赖项将是可取的.

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).

正如所解释的,这里没有组合.然而,这个词是模棱两可的:

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

  • 这可能意味着对象组合.这只是关于具有另一个类的字段的对象.
  • 这可能意味着 UML 组合.这是一种特殊的具有独家所有权的关联
  • 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

这篇关于它在创建另一个对象时创建的类和对象的UML关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆