简体   繁体   English

UML:Inheritance 模板类之间有参数依赖

[英]UML:Inheritance between template classes with parameter dependencies

Pardon my poor english.请原谅我糟糕的英语。 Just let me show you the situation what I'm trying to draw in UML class diagram.让我向您展示我试图在 UML class 图中绘制的情况。

template<typename TB1, typename TB2, typename TB3>
class Base { ... };

template<typename TD1, typename TD2>
class Derived : public Base<typename TD1, typename TD2, int> { .... };

I know how to draw UML class diagram when a class inherits generic class, as below( similar question was asked before ).我知道当 class 继承泛型 class 时如何绘制 UML class 图,如下所示( 之前问过类似的问题)。统一语言

But what should I do if base class's template paramter type is being set by derived class's template parameter type?但是如果派生类的模板参数类型设置了基类的模板参数类型怎么办? Just add another arrow indicating binded type depends on derived class's template parameter?只需添加另一个箭头指示绑定类型取决于派生类的模板参数?

In short简而言之

You could show the relationship between the template Base and the template Derived either with a parameter binding (like in your picture, but between two template classes) or inheritance between template classes.您可以使用参数绑定(如您的图片,但在两个模板类之间)或模板类之间的 inheritance 来显示模板Base和模板Derived之间的关系。

But neither alternative is completely accurate regarding the C++ and the UML semantics at the same time.但是,对于 C++ 和 UML 语义,这两种选择都不完全准确。 For this you would need to decompose the template inheritance into a binding and an inheritance.为此,您需要将模板 inheritance 分解为一个绑定和一个 inheritance。

More explanations更多解释

What does your C++ code mean?您的 C++ 代码是什么意思?

The C++ generalization between Derived and Base makes three things at once: DerivedBase之间的 C++ 泛化同时做了三件事:

  • it binds parameters of the Base template class (ie substituting TD1 for TB1 , TD2 for TB2 and int for TB3 );它绑定了Base模板 class 的参数(即用TD1代替TB1 ,用TD2代替TB2 ,用int代替TB3 );
  • it keeps TD1 and TD2 substituable in the resulting bound class;它使TD1TD2在生成的绑定 class 中保持可替换;
  • it creates a generalization between the classes obtained by binding the parameters.它在通过绑定参数获得的类之间创建了泛化。

For the readers who are less familiar with C++, let's illustrate this by using aliases to clarify:对于C++不太熟悉的读者,我们用别名来说明一下:

template<typename TB1, typename TB2, typename TB3>
class Base {  };

template<typename TD1, typename TD2>
class Derived : public Base<TD1, TD2, int> { };

int main() {
  using MyDerived = Derived<string, Test>; // class corresponding to binding parameters
  using MyBase = Base<string, Test, int>;  // also binding parameters
  MyBase *p = new MyDerived();         // this assignment works because the bound 
                                       // MyBase generalization is a generalization 
                                       // from MyDerived
}

So this code means that there is a generic specialization of Base into Derived which is true, whatever the parameter bindings, and in particular for the bound MyBase and MyDerived .所以这段代码意味着有一个从BaseDerived的通用特化,这是正确的,无论参数绑定如何,特别是对于绑定的MyBaseMyDerived

How to show it in UML?如何在UML中显示它?

Option 1 - binding选项 1 - 绑定

A first possibility is to simply use <<bind>> between template classes:第一种可能性是在模板类之间简单地使用<<bind>>

UML specs, section 9.3.3.1 : (...) the details of how the contents are merged into a bound element are left open. UML 规范,第 9.3.3.1 节:(...) 如何将内容合并到绑定元素中的详细信息保持开放状态。 (...) A bound Classifier may have contents in addition to those resulting from its bindings. (...) 绑定的分类器除了其绑定产生的内容外,还可能包含其他内容。

Derived would be a bound classifier obtained by binding parameters of Base and adding "own content", including redefinitions of base elements ("overrides"). Derived将是一个绑定分类器,通过绑定Base的参数并添加“自己的内容”,包括对基本元素的重新定义(“覆盖”)。 This is not wrong, but would not appropriately reflect that there is an inheritance also between bound classes obtained from Derived and bound classes obtained directly from Base .这没有错,但不能恰当地反映出从Derived获得的绑定类和直接从Base获得的绑定类之间也有一个 inheritance。

Option 2 - inheritance选项 2 - inheritance

Another approach could be inheritance between the templates:另一种方法可能是模板之间的 inheritance:

在此处输入图像描述

It corresponds to the C++ semantics.对应C++语义。 But the UML section 9.9.3.2 Template classifier specializations gives another semantic to this diagram:但是 UML 部分9.9.3.2 Template classifier specializations为该图提供了另一种语义:

A RedefinableTemplateSignature redefines the RedefinableTemplateSignatures of all parent Classifiers that are templates. RedefinableTemplateSignature 重新定义所有作为模板的父分类器的 RedefinableTemplateSignatures。 All the formal TemplateParameters of the extended (redefined) signatures are included as formal TemplateParameters of the extending signature, along with any TemplateParameters locally specified for the extending signature.扩展(重新定义)签名的所有正式 TemplateParameters 都包含为扩展签名的正式 TemplateParameters,以及为扩展签名本地指定的任何 TemplateParameters。

I understand this as meaning that the template parameters increase (ie the set would be TB1 , TB2 , TB3 , TD1 and TD2 ) and there is no semantics nor notation foreseen to define a local binding of some parents elements.我理解这意味着模板参数增加(即集合将是TB1TB2TB3TD1TD2 )并且没有语义或符号预见来定义某些父元素的本地绑定。 So UML readers might misunderstand the design intent.所以 UML 读者可能会误解设计意图。

Option 3 - binding and inheritance选项 3 - 绑定和 inheritance

The cleanest way would therefore be to decompose the binding and the inheritance (I've used a bound class that is itself templated with the new parameter name to align, but this could be overkill):因此,最简洁的方法是分解绑定和 inheritance(我使用了绑定 class,它本身使用新参数名称模板化以对齐,但这可能有点矫枉过正):

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM