UML 关联理解问题 [英] UML association understanding problem

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

问题描述

我已经使用 UML 有一段时间了,我已经阅读了一些关于它的文章、书籍和论坛,但我仍然不明白什么时候应该用关联线(一条简单的线或箭头(或这些不一样吗?)).我将提供三个例子——你能告诉我哪一个会导致这两个类处于这种关系中吗?

I've been using UML for a while and I've read few articles, books, forums about it but I still don't REALLY understand when two classes should be connected with association line (a simple line or arrow (or are these not the same?)). I'll provide three examples - can you tell me which one will cause the two classes to be in this relationship?

1.

//a field of OtherClass
    public class MainClass
    {
        private OtherClass other;
    }

2.

//method argument
    public class MainClass
    {
        public void Action(OtherClass other)
        { }
    }

3.

//method return value
    public class MainClass
    {
        public OtherClass Action()
        { }
    }

4.

//used inside a method
    public class MainClass
    {
        private Something something;

        public void Action()
        {
            OtherClass other = something.GetOtherClass();
        }
    }

推荐答案

首先一个箭头代表了关联的可导航性.单箭头表示单向关系,在这种情况下,只有源类知道目标类.两端的箭头表示双向关系,两个类都知道彼此.如果不存在箭头,则关联可以默认是双向的,或者为了可读性而被抑制.在实践中,只有当你想强调关联的方向时才应该画箭头.

First of all an arrow represents navigability of the association. Single arrow means unidirectional relation, in this case only the source class knows about the target class. Arrow on both ends means bidirectional relation, where both classes know about each other. If no arrows are present the association can be either bidirectional by default or suppressed for the sake of readability. In practice you should draw arrows only when you want to emphasize the direction of the association.

当谈到您的第二个问题时,只有第一种情况描述了 MainClassOtherClass 之间的(单向)关联.参数和返回值都没有暗示 UML 意义上的关联(尽管两者都暗示依赖).在最后一个示例中,通过 something 属性在 MainClassSomething 类之间存在关联.根据经验,您应该在属性中寻找关联.

When it comes to your second question, only the first case describes an (unidirectional) association between MainClass and OtherClass. Neither arguments nor return values imply association in the UML sense (although both imply dependency). In the last example there is an association between MainClass and Something class through the something attribute. As a rule of thumb you should look for associations in attributes.

请注意UML中有dependency的概念,用虚线表示.

Please note that there is a concept of dependency in UML and it is represented by a dashed line.

波兹德罗维尼亚!

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

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