Profile 图:Stereotype 与元数据

Profile 图:Stereotype 与元数据

puml.online

简介 / Introduction

Stereotype(类型构造型)是在类图元素上附加语义标签的机制,用于区分同一类型的不同角色——如「接口」「服务」「枚举」「实体」。PlantUML 与 Mermaid 都支持在类图中使用 <<stereotype>> 语法,但细节有所不同。本文对比两者的写法与渲染效果。

PlantUML 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
skinparam classAttributeIconSize 0

profile UserProfile {
stereotype Entity <<entity>>
stereotype Service <<service>>
stereotype Interface <<interface>>
}

class User as "用户" <<entity>>
class AuthService as "认证服务" <<service>>
interface IUserRepo as "用户仓库" <<interface>>

User -- IUserRepo
AuthService ..> IUserRepo
@enduml

PlantUML 的 profile 块用于声明一组 stereotype 名称,< <type> > 标注直接写在类名后。<<entity>>、<<service>>、<<interface>> 是常见约定,配合 skinparam 可控制渲染样式。

Mermaid 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classDiagram
class User {
<<entity>>
+String name
+int id
}
class AuthService {
<<service>>
+login()
+logout()
}
class IUserRepo {
<<interface>>
+findById()
+save()
}
User --> IUserRepo
AuthService ..> IUserRepo

Mermaid 的 <<stereotype>> 写在类块内部首行,等同于 PlantUML 的 < <type> > 标注。classDiagram 支持 <<interface>>、<<abstract>>、<<service>> 等常见构造型。

对比 / Side-by-side

特性 PlantUML Mermaid
声明位置 类名后 < <type> > 类块内首行 <<type>>
Profile 块 支持 profile {} 声明 不支持
skinparam 样式 支持定制字体、颜色 主题色由 CSS 控制
关系箭头 --、..>、`< –`

小结 / Wrap-up

两者核心语法一致——<<type>> 表示构造型——但 PlantUML 提供 profile 声明块和 skinparam 细粒度控制,适合复杂企业建模;Mermaid 胜在轻量和 Markdown 友好,适合文档内嵌即时预览。

  • 标题: Profile 图:Stereotype 与元数据
  • 作者: puml.online
  • 创建于 : 2026-10-21 09:00:00
  • 更新于 : 2026-10-21 01:07:06
  • 链接: https://puml.online/blog/profile-diagram-stereotypes/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。