Feature Description
This skill is specifically for the design and drawing of UML (Unified Modeling Language) diagrams, including:
- System architecture design
- Class diagrams and object diagrams
- Sequence diagrams and collaboration diagrams
- Use case diagrams and activity diagrams
- State diagrams and deployment diagrams
- Component diagrams and package diagrams
Usage Scenarios
- "Design a class diagram for an e-commerce system"
- "Draw a sequence diagram for user login"
- "Create a system architecture diagram"
- "Design a database ER diagram"
- "Draw a business process diagram"
- "Create a use case diagram to show system functionality"
UML Diagram Types
1. Class Diagram
Used to show the classes, attributes, methods, and their relationships in a system.
@startuml
class User {
- id: Long
- username: String
- email: String
- password: String
+ login(): boolean
+ logout(): void
+ updateProfile(): void
}
class Order {
- id: Long
- orderNumber: String
- totalAmount: Decimal
- status: OrderStatus
+ create(): void
+ cancel(): void
+ pay(): boolean
}
class Product {
- id: Long
- name: String
- price: Decimal
- stock: Integer
+ updateStock(quantity: Integer): void
+ getPrice(): Decimal
}
User "1" -- "*" Order : places
Order "*" -- "*" Product : contains
@enduml
Relationship Types:
- Association: solid line, indicates a relationship between classes
- Aggregation: hollow diamond, indicates a whole-part relationship
- Composition: filled diamond, indicates a strong whole-part relationship
- Inheritance: hollow triangle arrow, indicates an is-a relationship
- Realization: dashed line with hollow triangle arrow, indicates interface implementation
- Dependency: dashed arrow, indicates a usage relationship
2. Sequence Diagram
Shows the interaction sequence and message passing between objects.
@startuml
actor User
participant "Web Browser" as Browser
participant "API Server" as API
participant "Database" as DB
User -> Browser: 输入登录信息
Browser -> API: POST /api/login
activate API
API -> DB: 查询用户信息
activate DB
DB --> API: 返回用户数据
deactivate DB
alt 验证成功
API -> API: 生成 JWT Token
API --> Browser: 返回 Token
Browser --> User: 登录成功
else 验证失败
API --> Browser: 返回错误信息
Browser --> User: 显示错误提示
end
deactivate API
@enduml
Element Description:
- Actor: participant (user or external system)
- Participant: object or component
- Message: message passing (synchronous/asynchronous)
- Activation: object activation period
- Alt/Opt/Loop: condition, optional, loop
3. Use Case Diagram
Shows the relationship between system functions and participants.
@startuml
left to right direction
actor 用户 as User
actor 管理员 as Admin
rectangle 电商系统 {
usecase "浏览商品" as UC1
usecase "搜索商品" as UC2
usecase "添加到购物车" as UC3
usecase "下单购买" as UC4
usecase "支付订单" as UC5
usecase "管理商品" as UC6
usecase "查看订单" as UC7
usecase "处理退款" as UC8
}
User --> UC1
User --> UC2
User --> UC3
User --> UC4
User --> UC5
User --> UC7
Admin --> UC6
Admin --> UC7
Admin --> UC8
UC4 ..> UC3 : <<include>>
UC5 ..> UC4 : <<include>>
@enduml
Relationship Types:
- Association: relationship between participants and use cases
- include: use case that must be executed
- extend: use case that may optionally be executed
- Generalization: inheritance relationship between use cases
4. Activity Diagram
Shows the execution flow of a business process or algorithm.
@startuml
start
:用户提交订单;
if (库存充足?) then (是)
:锁定库存;
:创建订单;
fork
:发送确认邮件;
fork again
:更新库存;
fork again
:记录日志;
end fork
:等待支付;
if (支付成功?) then (是)
:确认订单;
:安排发货;
else (否)
:取消订单;
:释放库存;
endif
else (否)
:提示库存不足;
endif
stop
@enduml
Element Description:
- Start/End: start and end points of the process
- Activity: operation being executed
- Decision: condition judgment
- Fork/Join: parallel execution
- Swimlane: responsibility division
5. State Diagram
Shows the state changes of an object during its lifecycle.
@startuml
[*] --> 待支付 : 创建订单
待支付 --> 已支付 : 支付成功
待支付 --> 已取消 : 超时/用户取消
已支付 --> 配货中 : 开始配货
配货中 --> 已发货 : 发货
已发货 --> 已完成 : 确认收货
已发货 --> 退款中 : 申请退款
退款中 --> 已退款 : 退款成功
退款中 --> 已发货 : 拒绝退款
已完成 --> [*]
已取消 --> [*]
已退款 --> [*]
待支付 : entry / 锁定库存
待支付 : exit / 释放库存
已支付 : entry / 扣减库存
@enduml
6. Component Diagram
Shows the component structure and dependency relationships of a system.
@startuml
package "前端层" {
[Web 应用]
[移动应用]
}
package "API 层" {
[API Gateway]
[认证服务]
}
package "业务层" {
[用户服务]
[订单服务]
[商品服务]
[支付服务]
}
package "数据层" {
database "MySQL" as DB
database "Redis" as Cache
queue "消息队列" as MQ
}
[Web 应用] --> [API Gateway]
[移动应用] --> [API Gateway]
[API Gateway] --> [认证服务]
[API Gateway] --> [用户服务]
[API Gateway] --> [订单服务]
[API Gateway] --> [商品服务]
[订单服务] --> [支付服务]
[订单服务] --> DB
[用户服务] --> DB
[商品服务] --> DB
[用户服务] --> Cache
[商品服务] --> Cache
[订单服务] --> MQ
[支付服务] --> MQ
@enduml
7. Deployment Diagram
Shows the physical deployment architecture of a system.
@startuml
node "用户设备" {
[浏览器]
[移动 App]
}
node "负载均衡器" {
[Nginx]
}
node "应用服务器集群" {
node "服务器 1" {
[Node.js 实例 1]
}
node "服务器 2" {
[Node.js 实例 2]
}
}
node "数据库服务器" {
database "MySQL 主库"
database "MySQL 从库"
}
node "缓存服务器" {
database "Redis 集群"
}
node "文件存储" {
[OSS/S3]
}
[浏览器] --> [Nginx]
[移动 App] --> [Nginx]
[Nginx] --> [Node.js 实例 1]
[Nginx] --> [Node.js 实例 2]
[Node.js 实例 1] --> [MySQL 主库]
[Node.js 实例 2] --> [MySQL 主库]
[MySQL 主库] --> [MySQL 从库] : 主从复制
[Node.js 实例 1] --> [Redis 集群]
[Node.js 实例 2] --> [Redis 集群]
[Node.js 实例 1] --> [OSS/S3]
[Node.js 实例 2] --> [OSS/S3]
@enduml
Drawing Tools
PlantUML
The most popular text-driven UML tool.
Installation:
# 使用 npm
npm install -g node-plantuml
# 使用 Java
# 下载 plantuml.jar
java -jar plantuml.jar diagram.puml
Online Tools:
- PlantUML Online Server
- PlantText
- PlantUML QEditor
Mermaid
Markdown-friendly diagram tool.
classDiagram
class User {
+String username
+String email
+login()
+logout()
}
class Order {
+String orderNumber
+Decimal amount
+create()
+cancel()
}
User "1" --> "*" Order : places
Other Tools
- Draw.io/diagrams.net:Online diagramming tools
- Lucidchart:Professional diagramming software
- Visual Paradigm:Enterprise-level modeling tools
- StarUML:Open-source UML tools
Design Best Practices
1. Class Diagram Design
✓ 遵循单一职责原则
✓ 使用清晰的命名
✓ 标注关键属性和方法
✓ 显示重要的关系
✓ 使用包组织大型系统
✗ 避免过度设计
✗ 不要包含所有细节
✗ 避免循环依赖
2. Sequence Diagram Design
✓ 从左到右排列参与者
✓ 按时间顺序排列消息
✓ 使用清晰的消息名称
✓ 标注重要的条件和循环
✓ 保持图表简洁
✗ 避免过多的参与者
✗ 不要遗漏返回消息
✗ 避免交叉的消息线
3. Use Case Diagram Design
✓ 使用用户视角描述功能
✓ 保持用例粒度适中
✓ 明确参与者角色
✓ 标注关键的关系
✓ 按功能模块组织
✗ 避免技术术语
✗ 不要过于细化
✗ 避免遗漏重要用例
Practical Application Scenarios
E-commerce System Design
@startuml
' 核心领域模型
class User {
- id: Long
- username: String
- email: String
- phone: String
+ register()
+ login()
+ updateProfile()
}
class Product {
- id: Long
- name: String
- description: String
- price: Decimal
- stock: Integer
+ updateStock()
+ getPrice()
}
class Order {
- id: Long
- orderNumber: String
- totalAmount: Decimal
- status: OrderStatus
- createdAt: DateTime
+ create()
+ cancel()
+ pay()
+ ship()
}
class OrderItem {
- id: Long
- quantity: Integer
- price: Decimal
+ calculateSubtotal()
}
class Payment {
- id: Long
- amount: Decimal
- method: PaymentMethod
- status: PaymentStatus
+ process()
+ refund()
}
class Address {
- id: Long
- province: String
- city: String
- detail: String
- phone: String
}
enum OrderStatus {
PENDING
PAID
SHIPPED
COMPLETED
CANCELLED
}
enum PaymentMethod {
ALIPAY
WECHAT
CREDIT_CARD
}
User "1" -- "*" Order : places
User "1" -- "*" Address : has
Order "1" *-- "*" OrderItem : contains
OrderItem "*" -- "1" Product : references
Order "1" -- "1" Payment : pays with
Order "1" -- "1" Address : ships to
@enduml
Microservices Architecture Diagram
@startuml
!define RECTANGLE class
package "客户端" {
[Web 前端]
[移动 App]
}
package "API 网关层" {
[API Gateway]
[认证中心]
}
package "微服务层" {
RECTANGLE "用户服务" {
[用户管理]
[权限管理]
}
RECTANGLE "商品服务" {
[商品管理]
[库存管理]
}
RECTANGLE "订单服务" {
[订单管理]
[订单状态]
}
RECTANGLE "支付服务" {
[支付处理]
[退款处理]
}
}
package "基础设施层" {
database "MySQL 集群"
database "Redis 集群"
queue "Kafka"
[配置中心]
[服务注册]
}
[Web 前端] --> [API Gateway]
[移动 App] --> [API Gateway]
[API Gateway] --> [认证中心]
[API Gateway] --> [用户管理]
[API Gateway] --> [商品管理]
[API Gateway] --> [订单管理]
[订单管理] --> [支付处理]
[订单管理] --> [库存管理]
[用户管理] --> [MySQL 集群]
[商品管理] --> [MySQL 集群]
[订单管理] --> [MySQL 集群]
[用户管理] --> [Redis 集群]
[商品管理] --> [Redis 集群]
[订单管理] --> [Kafka]
[支付处理] --> [Kafka]
[用户管理] --> [配置中心]
[商品管理] --> [配置中心]
[订单管理] --> [配置中心]
[用户管理] --> [服务注册]
[商品管理] --> [服务注册]
[订单管理] --> [服务注册]
@enduml
Documentation Integration
Embedding in Markdown
# 系统设计文档
## 类图
\`\`\`plantuml
@startuml
class User {
+login()
}
@enduml
\`\`\`
## 时序图
\`\`\`mermaid
sequenceDiagram
User->>API: 请求
API->>DB: 查询
DB-->>API: 返回
API-->>User: 响应
\`\`\`
Using in Code Comments
/**
* 用户服务类
*
* @startuml
* class UserService {
* +register(user: User): boolean
* +login(username: String, password: String): Token
* }
* @enduml
*/
public class UserService {
// 实现代码
}
Considerations
- Choose the appropriate diagram type
- Keep diagrams concise and clear
- Use consistent naming conventions
- Update documentation in a timely manner
- Consider the readability of diagrams
- Avoid over-design
- Use version control to manage diagram files
- Maintain communication with the team