C语言 C语言的UML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2042608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:07:25  来源:igfitidea点击:

UML for C programming language

cuml

提问by Andy

UML is most commonly used for modelling system by using C++. In my projects C is the implementation language. I am looking for resources on UML strategies which are applicable for C. I want to use UML during design and represent the different aspects of the system.

UML 最常用于使用 C++ 对系统进行建模。在我的项目中,C 是实现语言。我正在寻找有关适用于 C 的 UML 策略的资源。我想在设计期间使用 UML 并代表系统的不同方面。

回答by Emile Cormier

I don't know of any existing resources that discuss using UML specifically for C. As others have mentioned, UML is language-agnostic.

我不知道有任何现有资源专门讨论将 UML 用于 C。正如其他人所提到的,UML 与语言无关。

Keep in mind that with UML, you can have a model for the problem domain, and another for the implementation. Try not to model the problem domain in terms of C, but rather as high-level OO. Once you understand the problem domain adequately enough, you can begin modeling an implementation.

请记住,使用 UML,您可以拥有一个用于问题域的模型,以及另一个用于实现的模型。尽量不要根据 C 对问题域进行建模,而是将其建模为高级 OO。一旦您充分了解问题域,就可以开始对实现进行建模。

For modeling procedural-style C implementations, the following diagrams could be useful:

对于过程式 C 实现的建模,下图可能很有用:

  • Class diagram:
    • Show C module APIs
    • Show C module relationships (mostly dependencies for non-OO)
    • Show structures and enumerations (using < < stereotype> >)
  • Package diagram: Show contents (modules) of libraries, and the dependency relationships between libraries
  • Activity diagram: Flowcharting non-trivial algorithms
  • Sequence/collaboration diagram: Show how events/messages between modules/entities/inputs/outputs occur in time
  • Statechart diagram: For state machines, of course!
  • 类图:
    • 显示 C 模块 API
    • 显示 C 模块关系(主要是非 OO 的依赖关系)
    • 显示结构和枚举(使用<<构造型>>)
  • 包图:展示库的内容(模块),以及库之间的依赖关系
  • 活动图:流程图非平凡算法
  • 序列/协作图:显示模块/实体/输入/输出之间的事件/消息如何及时发生
  • 状态图:当然是状态机!

Expanding on class diagrams, you can "abuse" them in the following way for procedural-style C:

扩展类图,您可以通过以下方式“滥用”它们以用于程序风格的 C:

  • Global extern functions -> public methods
  • Local static functions -> private methods
  • Global extern variables -> public members
  • Local static variables -> private members
  • Structs -> class with "struct" stereotype
  • #defineconstants -> class with "enumeration" stereotype
  • 全局外部函数 -> 公共方法
  • 本地静态函数 -> 私有方法
  • 全局外部变量 -> 公共成员
  • 局部静态变量 -> 私有成员
  • 结构 -> 带有“结构”构造型的类
  • #define常量 -> 带有“枚举”构造型的类

Experiment, and you'll find your own conventions for abusing UML.

试验一下,你会发现你自己的滥用 UML 的约定。

回答by monksy

The problem with C is that it is more of a procedural programming language. Its harder to get the fine grained design with a C application. If you are working with C you may want to stick with sequence diagrams, and component diagrams, as that they describe and overview of what is going on rather than a graph of dependencies and interaction.

C 的问题在于它更像是一种过程编程语言。使用 C 应用程序更难获得细粒度的设计。如果您正在使用 C,您可能希望坚持使用序列图和组件图,因为它们描述和概述了正在发生的事情,而不是依赖关系和交互的图。

回答by jdehaan

An object oriented design is independent of the language and you can of course design your system using UML. Some tools like Rhapsody will also allow code generation and round-tripping, we use for some special projects where C++ is not an option. If you want to write your code by hand use some naming convention like Subsystem_Module_Class_Method to name your functions in an object oriented way and use a .c file per class. Using C is not an obstacle against having a clean design.

面向对象的设计独立于语言,您当然可以使用 UML 设计您的系统。像 Rhapsody 这样的一些工具也允许代码生成和往返,我们用于一些 C++ 不可用的特殊项目。如果您想手动编写代码,请使用一些命名约定,如 Subsystem_Module_Class_Method 以面向对象的方式命名您的函数,并为每个类使用一个 .c 文件。使用 C 并不是拥有干净设计的障碍。