UML 关联和依赖 [英] UML association and dependency

查看:30
本文介绍了UML 关联和依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关联和依赖有什么区别?你能给出代码示例吗?A班和B班是什么关系?

What is the difference between association and dependency? Can you give code examples? What is the relationship between class A and B?

class A
{
    B *b;

    void f ()
    {
        b = new B ();
        b->f();
        delete b;
    }
}

推荐答案

简短的回答是:没有严格定义在 UML 中应该如何表示任何特定的源语言结构.这将是所讨论语言的标准化 UML 配置文件的一部分,但遗憾的是,这些文件很少而且相距甚远.长答案如下.

The short answer is: how any specific source language construct should be represented in UML is not strictly defined. This would be part of a standardized UML profile for the language in question, but these are sadly few and far between. Long answer follows.

在你的例子中,恐怕我不得不说都不是",只是为了很难.A 有一个 B 类型的成员变量,所以这个关系实际上是一个聚合或者一个组合……或者一个有向关联.在 UML 中,与命名目标角色的定向关联在语义上等同于具有相应名称的属性.

In your example, I'm afraid I would have to say "neither", just to be difficult. A has a member variable of type B, so the relationship is actually an aggregation or a composition... Or a directed association. In UML, a directed association with a named target role is semantically equivalent to an attribute with the corresponding name.

根据经验,如果 bA 的构造函数中被初始化,它就是一个聚合;如果它也在 B 的析构函数(共享生命周期)中被销毁,则它是一个组合.如果两者都不适用,则为属性/定向关联.

As a rule of thumb, it's an aggregation if b gets initialized in A's constructor; it's a composition if it also gets destroyed in B's destructor (shared lifecycle). If neither applies, it's an attribute / directed association.

如果 b 不是 A 中的成员变量,并且局部变量 b 没有被操作(没有方法被调用)),然后我会将其表示为依赖项:A 需要 B,但它没有该类型的属性.

If b was not a member variable in A, and the local variable b was not operatoed on (no methods were called on it), then I would represent that as a dependency: A needs B, but it doesn't have an attribute of that type.

但是f()实际上调用了B中定义的方法.对我来说,这使正确的关系成为 <<use>>,这是一种更特殊的依赖形式.

But f() actually calls a method defined in B. This to me makes the correct relationship a <<use>>, which is a more specialized form of dependency.

最后,(无向)关联是两个类之间最弱的链接形式,因此我倾向于在描述源结构时不使用它们.当我这样做时,我通常在没有直接的源代码关系时使用它们,但是这两个类仍然有某种关联.这方面的一个例子可能是这样一种情况,即两者负责同一个更大算法的不同部分,但第三类同时使用它们.

Finally, an (undirected) association is the weakest form of link between two classes, and for that very reason I tend not to use them when describing source constructs. When I do, I usually use them when there are no direct source code relationships, but the two classes are still somehow related. An example of this might be a situation where the two are responsible for different parts of the same larger algorithm, but a third class uses them both.

这篇关于UML 关联和依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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