掌握UML類圖:使用PlantUML進行系統設計的實務案例研究
引言
在今日複雜的軟體開發環境中,清晰的溝通與精確的系統建模對於專案成功至關重要。在軟體架構師工具箱中,最強大的工具之一是UML類圖——一種視覺化語言,能夠彌補抽象需求與具體實作之間的差距。
本案例研究探討類圖如何作為物件導向設計的骨幹,使團隊能夠建模系統的靜態結構,定義實體之間的關係,並建立明確的開發合約。透過一個實際的電子商務訂單管理系統範例,我們將示範如何在三個開發視角——概念性、規格性與實作性——中逐步優化類圖,同時利用PlantUML來建立可執行且版本控制的文件。
無論您是業務分析師在建模領域概念、開發人員設計API,還是團隊主管確保架構一致性,本指南都提供具體實用的洞察,協助您建立能提升清晰度、減少歧義並加速交付的類圖。
理解類圖:核心概念回顧
(精簡自基礎知識)
一個類圖在UML中是一種靜態結構圖,用以呈現:
-
類別:定義物件的藍圖,包含屬性(狀態)與操作(行為)
-
關係:繼承、關聯、聚合、組合與依賴
-
約束:可見性(
+,-,#,~),多重性(1,0..*,1..5),以及可導航性
關鍵符號元素

@startuml
class Order {
-orderId: String
-orderDate: Date
+calculateTotal(): Double
+addItem(item: Product, qty: int): void
}
@enduml
關係類型快速參考
| 類型 | 符號 | 含義 | 範例 |
|---|---|---|---|
| 繼承 | `– | >` | 「是-a」 |
| 關聯 | -- |
結構連結 | Order -- Customer |
| 聚合 | o-- |
「有-a」(弱) | Warehouse o-- Product |
| 組成 | *-- |
「擁有-a」(強) | Order *-- OrderItem |
| 依賴 | ..> |
「使用」(暫時) | PaymentService ..> Logger |
案例研究:電子商務訂單管理系統
業務需求
一家線上零售商需要一個系統來:
-
管理客戶、產品和訂單
-
支援訂單項目,包含數量與定價
-
處理多種付款方式
-
追蹤訂單狀態的整個生命週期
-
允許產品屬於分類
-
支援訪客結帳(可選的客戶關聯)
第一階段:概念模型(領域觀點)
語言無關,專注於現實世界的概念

@startuml
title 概念模型:電子商務領域
class Customer {
名稱
電子郵件
配送地址
}
class Product {
名稱
描述
基礎價格
}
class Category {
名稱
描述
}
class Order {
訂單編號
訂單日期
狀態
總金額
}
class OrderItem {
數量
單價
小計
}
class Payment {
付款方式
交易編號
金額
時間戳記
}
' 關係
Customer "1" -- "0..*" Order : 下單 >
Order "1" *-- "1..*" OrderItem : 包含 >
Product "1" -- "0..*" OrderItem : 出現在 >
Product "0..*" -- "1" Category : 屬於 >
Order "1" -- "1..*" Payment : 由...結清 >
note right of Order
訂單代表客戶的購買意圖與交易
end note
@enduml
關鍵設計決策:
-
組合(
*--)之間的訂單與訂單項目:項目無法在沒有訂單的情況下存在 -
之間的關聯
產品與類別: 產品可以重新分類 -
多重性
0..*適用於客戶-訂單:支援訪客結帳
第二階段:規格模型(介面觀點)
專注於軟體合約,隱藏實作細節

@startuml
title 規格模型:服務介面
interface IOrderService {
+createOrder(customerId: String, items: List<OrderItemDTO>): OrderDTO
+getOrder(orderId: String): OrderDTO
+updateOrderStatus(orderId: String, status: OrderStatus): boolean
+calculateOrderTotal(orderId: String): Money
}
interface IPaymentProcessor {
+processPayment(orderId: String, paymentDetails: PaymentDTO): PaymentResult
+refundPayment(transactionId: String, amount: Money): RefundResult
}
interface IInventoryService {
+checkAvailability(productId: String, quantity: int): boolean
+reserveItems(orderId: String, items: List<ReservationItem>): boolean
+releaseReservation(orderId: String): void
}
class OrderDTO {
+orderId: String
+customerId: String
+items: List<OrderItemDTO>
+total: Money
+status: OrderStatus
}
class OrderItemDTO {
+productId: String
+quantity: int
+unitPrice: Money
}
' 依賴關係
IOrderService ..> IInventoryService : 使用 >
IOrderService ..> IPaymentProcessor : 協調 >
IOrderService ..> OrderDTO : 回傳 >
note bottom of IOrderService
定義訂單管理的合約。
實作可能不同(微服務、單體等)
end note
@enduml
架構優勢:
-
介面分離支援獨立部署
-
DTO 可將內部模型與 API 合約分離
-
依賴關係清楚顯示微服務的服務邊界
第三階段:實作模型(程式碼觀點)
針對 Java/Spring Boot 實作的技術特定細節

@startuml
title 實作模型:Java/Spring Boot 類別
package com.ecommerce.order.entity {
class Order {
-@Id orderId: UUID
-@ManyToOne customer: Customer
-@OneToMany(cascade=ALL) items: List<OrderItem>
-orderDate: LocalDateTime
-status: OrderStatus
-totalAmount: BigDecimal
+addItem(product: Product, qty: int): void
+calculateTotal(): BigDecimal
+markAsShipped(): void
}
class OrderItem {
-@Id itemId: UUID
-@ManyToOne order: Order
-@ManyToOne product: Product
-quantity: int
-unitPrice: BigDecimal
+getSubtotal(): BigDecimal
}
enum OrderStatus {
PENDING
CONFIRMED
SHIPPED
DELIVERED
CANCELLED
}
}
package com.ecommerce.payment.service {
class PaymentService {
-@Autowired paymentGateway: PaymentGateway
-@Autowired orderRepository: OrderRepository
+processPayment(orderId: UUID, dto: PaymentRequest): PaymentResponse
-validatePaymentDetails(dto: PaymentRequest): void
-updateOrderPaymentStatus(orderId: UUID, status: PaymentStatus): void
}
interface PaymentGateway {
+charge(amount: BigDecimal, card: CardDetails): TransactionResult
+refund(transactionId: String, amount: BigDecimal): RefundResult
}
}
' 關係
Order "1" *-- "1..*" OrderItem : 組合 >
Order ..> PaymentService : 依賴於 >
PaymentService ..> PaymentGateway : 透過實作 >
note right of OrderItem
@Entity 標籤對應至資料庫表格。
Cascade=ALL 確保項目與訂單一同持久化。
end note
@enduml
實作重點:
-
JPA 標籤(
@Entity,@ManyToOne)用於 ORM 映射 -
相依性注入(
@Autowired)用於鬆散耦合 -
用於類型安全訂單狀態管理的列舉
-
私有輔助方法(
-驗證付款詳情)封裝邏輯
進階模式與最佳實務
1. 處理可見性與封裝

@startuml
class BankAccount {
+accountNumber: String
+getBalance(): BigDecimal
-balance: BigDecimal
-transactionHistory: List<Transaction>
#calculateInterest(rate: double): BigDecimal
~internalAudit(): void
}
note right of BankAccount
+ 公開:外部客戶的 API
- 私有:內部狀態,無法由外部存取
# 受保護:用於子類別擴展
~ 套件:在同一模組內可見
end note
@enduml
2. 實際情境中的多重性

@startuml
class ShoppingCart {
+addItem(product: Product, qty: int): void
+removeItem(productId: String): boolean
}
class Product {
+name: String
+price: BigDecimal
+inStock: boolean
}
' 購物車可以包含 0 到多個項目
' 每個項目僅參考一個產品
ShoppingCart "1" *-- "0..*" Product : 包含 >
note bottom
多重性規則:
• 0..* = 可選,多個(最常見)
• 1 = 嚴格一個(必要)
• 0..1 = 可選,單一(例如:個人頭像)
• 1..* = 至少一個(例如:訂單項目)
end note
@enduml
3. 抽象類別與介面

@startuml
abstract class Notification {
#recipient: String
#message: String
+abstract send(): boolean
+logDelivery(): void
}
interface EmailNotification {
+subject: String
+send(): boolean
}
interface SMSNotification {
+phoneNumber: String
+send(): boolean
}
Notification <|-- EmailNotification
Notification <|-- SMSNotification
note right of Notification
抽象類別:共享狀態 + 部分實作
介面:純合約,支援多重繼承
end note
@enduml
常見陷阱與避免方法
| 陷阱 | 症狀 | 解決方案 |
|---|---|---|
| 過度設計 | 包含 50 個以上類別的圖表,難以閱讀 | 從概念模型開始;根據界限上下文將其拆分為多個圖表 |
| 混淆聚合/組合 | 物件生命週期管理不清晰 | 提問:「如果整體被銷毀,部分是否仍存在?」若否 → 使用組合(*--) |
| 忽略可導航性 | 到處都是雙向箭頭 | 僅在程式碼中需要遍歷的位置添加可導航性箭頭 |
| 混合抽象層級 | DTO 與實體類別混合在同一張圖表中 | 依觀點分離圖表(概念/規格/實作) |
| 忽略版本控制 | 圖表變得過時 | 在 Git 中使用 PlantUML 文字檔;於 CI/CD 管道中產生影像 |
工具建議:為什麼選擇 PlantUML?
針對上述案例,PlantUML被選用的原因是因為它:
✅ 基於文字:圖表即是程式碼——可版本化、可比對、可審查
✅ 可攜帶:可在本機或透過雲端服務渲染;可與 Confluence、GitHub、VS Code 整合
✅ 易於維護:更新圖表邏輯時無需重新繪製框格
✅ 協作性佳:非設計人員可透過簡單語法參與貢獻
範例工作流程:
# 1. 以文字形式撰寫圖表
echo '@startumlnclass User { +name: String }n@enduml' > UserDiagram.puml
# 2. 產生 PNG/SVG 檔案
plantuml -tpng UserDiagram.puml
# 3. 將 .puml 檔案與產生的影像一併提交至 Git
git add UserDiagram.puml UserDiagram.png
結論
類別圖遠不止於學術練習——它們是活生生的實體,能促進共識、減少技術負債,並加速整個軟體開發生命週期中的上手過程。正如我們在電商案例研究中所展示的,類別圖的真正力量在於它們透過三個關鍵視角不斷演進時才會顯現出來:
🔹 概念:讓利害關係人建立對共同領域的共識
🔹 規格:為模組化架構定義清晰的介面
🔹 實作:以精確且具技術意識的藍圖引導開發人員
透過採用PlantUML透過採用 PlantUML 推行圖形即程式碼的實務,團隊能獲得靈活度,讓設計與程式碼同步迭代,確保文件永遠不會落後於實作。請記住:最好的類別圖並非最詳細的那一個——而是能在正確時機,為正確的對象回答正確問題的圖。
最終要點:從簡單開始,與利害關係人驗證,逐步優化,並始終將圖形元素與具體的商業價值連結起來。當類別圖轉變為協作工具而非交付成果時,它們便從負擔轉化為打造更優質軟體的催化劑。














