如何在UML方法中表示泛型参数? [英] How to represent generic parameter in UML method?

查看:292
本文介绍了如何在UML方法中表示泛型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将Java应用程序中的某些类逆向工程为UML 2类图。到目前为止,我已经找到了如何为Jon Skeet提出的全班课程模板表示:正确的方法是什么用UML表示模板类?。有了这些信息,我反向设计了这样一个类:

I have to reverse engineer some classes from a Java application into a UML 2 class diagram. So far so good, I have found how to represent class templates for the whole class as proposed by Jon Skeet here: What is the correct way to represent template classes with UML?. With this info, I have reverse engineered a class like this:

public class Foo<T> {
    //class fields and methods...
}

现在我尝试对只有一个方法包含泛型参数的类进行逆向工程是一个两难的选择:

Now I have a dilemma trying to reverse engineer a class which only a method contains a generic parameter:

public class OtherFoo {
    public <T extends Comparable<T>> boolean bar(T x, T y) {
        //fancy code goes here...
    }
}

你知道如何实现任何UML 2工具吗?我只是想理解这个概念。

Do you know how to achieve regardless any UML 2 tool? I just want to understand the concept.

推荐答案

我不知道如何在你选择的工具中做到这一点,但是在模型级别,它的工作方式与类完全相同。您可以使用签名创建模板操作。

I don't know how to do this in your tool of choice, but on the model level, it works exactly like for classes. You create a template operation with your signature.

UML2上层建筑的第17.4.14章为表示法指定:


模板操作的模板参数和模板参数绑定是两个列表之间的名称
操作和操作参数。

*< visibility>< name>'<'< template-parameter-list>'>''<<'< ; binding-expression-list>'>>''('< parameter>
[','< parameter>] ** ')'[':'< property-string>]

在你的情况下,让我们先看看

In your case, let's first see the simple case of

public <T> boolean bar(T x, T y)

这相当于


+ bar< T>(x:T,y:T):Boolean

+ bar<T> (x: T, y: T) : Boolean

您的原始示例看起来有点复杂,因为模板参数被约束到另一个类Comparable,而另一个类也是一个模板,其参数(我称之为T1)依次绑定到操作的参数。这给了我们

Your original example looks a bit more complicated because the template parameter is constrained to another class, Comparable, that in turn is also a template whose parameter (I'll call it T1) is bound to the operation's parameter in turn. This gives us


+ bar< T> Comparable< T1-> T >>(x:T,y:T):Boolean

+ bar<T > Comparable<T1->T>> (x: T, y: T) : Boolean


注意:前面的一些深度漫游)UML(以及某种程度上的C ++)指定的模板与Java中的Generics完全不同。它们看起来或多或少相同,但它们的语义中有些 - 有时是微妙的 - 差异可能使它们难以匹配。 UML中最重要的一个是:

Note: (A bit of in-depth rambling ahead) Templates as specified by UML (and to some degree C++) are a very different beast from Generics in Java. They look more or less the same, but there are - sometimes subtle - differences in their semantics that can make it difficult to match the two. The most important one in UML is this:


模板不能以与同类非模板元素相同的方式使用。模板元素
只能用于生成绑定元素(例如,模板类不能用作类型化元素的类型)或者作为另一个模板规范的部分
(例如,模板) class可以专门化另一个模板类)。

A template cannot be used in the same manner as a non-template element of the same kind. The template element can only be used to generate bound elements (e.g., a template class cannot be used as the type of a typed element) or as part of the specification of another template (e.g., a template class may specialize another template class).

这意味着在UML中,OtherFoo也需要是一个模板 - 即有一个模板签名(带0个参数)。为了在模板范围之外正确使用操作模板 - 即在活动或类似操作中调用它 - 首先必须将其绑定到具体操作,而不是使用它。如果您的示例,这意味着:

This means that in UML, OtherFoo needs to be a template, too - i.e. have a template signature (with 0 parameters). In order to then use the operation template correctly outside the template scope - i.e. call it in an activity or similar - you'd first have to bind it to a concrete operation, which is used instead. In case of your example, this means:


  1. 将OtherFoo模板绑定到(匿名)绑定类。

  2. 将绑定栏操作模板绑定到绑定类中的操作。

这篇关于如何在UML方法中表示泛型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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