简体   繁体   English

如何在 UML 类图中绘制 C++ 概念?

[英]How can I draw C++ concepts in UML class diagram?

How would I draw C++ concepts in an UML class diagram?我将如何在 UML 类图中绘制 C++ 概念?

Specifically, I have the following code:具体来说,我有以下代码:

template<typename T>
concept Printable = requires(T a, std::ostream &where) {
    { where << a };
};

template<typename T>
concept Identifiable = requires(T a) {
    { a.getId() } -> std::convertible_to<std::string>;
};

template<typename T>
concept Listable = Identifiable<T> && Printable<T>;

and then a class:然后是一个类:

template<Listable T>
class Liste {
    ...
    void add(T *data);
    ...
}

If it were a regular template, I would just put the T in a square in the corner of the class.如果是常规模板,我会将T放在班级角落的正方形中。 But what about the concepts?但是概念呢?

The C++ concepts defines constraints on the types associated with a template class: C++ 概念定义了对与模板类关联的类型的约束:

  • Since UML supports class template , you would typically express this constraints for its parameters, between curly brackets, either in natural language or in OCL.由于 UML 支持类模板,您通常会在大括号之间用自然语言或 OCL 对其参数表达此约束 The former is perfect at an early design stage.前者在早期设计阶段是完美的。 But you could as well consider to pragmatically express it using the same syntax than C++ instead of natural language, if you need this level of accuracy.但是,如果您需要这种级别的准确性,您也可以考虑使用与 C++ 相同的语法而不是自然语言来务实地表达它。

  • Alternatively, you may as well handle the C++ concept as a kind of generic type in UML.或者,您也可以将 C++ 概念作为 UML 中的一种泛型类型来处理。 You could then use the concept in a much more readable way in the the type definition of the template parameters in class templates.然后,您可以在类模板中模板参数的类型定义中以更具可读性的方式使用该概念。 This would be more convenient to read, and closer to the C++ idea.这会更方便阅读,并且更接近 C++ 的想法。 The problem is that nothing is foreseen in UML to define such generic types.问题是在 UML 中没有预见到定义这样的泛型类型。 You may therefore extend UML with an and-hoc profile to use a «concept» stereotype .因此,您可以使用 and-hoc配置文件扩展 UML 以使用«concept»构造型 You would then define concepts exactly like classes, define the constraints using the UML constraints , and use the concepts in UML class templates .然后您将完全像类一样定义概念,使用UML 约束定义约束,并使用 UML类模板中的概念。

Here, how the second approach would look like.在这里,第二种方法会是什么样子。 Note the realization dependency between two concepts:注意两个概念之间的实现依赖

在此处输入图片说明

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

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