Skip to content

UML 类图速查 ​

类的表示 ​

txt
┌─────────────────┐
│     ClassName    │
├─────────────────┤
│ - privateField  │
│ # protectedField│
│ + publicField   │
├─────────────────┤
│ - privateMethod()│
│ # protectedMethod()│
│ + publicMethod() │
└─────────────────┘

关系符号 ​

关系符号说明示例
继承──▷子类 extends 父类Dog ──▷ Animal
实现─ ─▷类 implements 接口Dog ─ ─▷ Pet
关联──→对象间的结构性关系,可表达导航方向Order ──→ Customer
聚合◇──→整体-部分,可独立存在Team ◇──→ Player
组合◆──→整体-部分,不可独立存在House ◆──→ Room
依赖─ ─→定义或行为上使用另一元素Service ─ ─→ Repository

关系强度 ​

txt
依赖 < 关联 < 聚合 < 组合
(弱)                (强)

上述排序只是教学性的耦合强度近似,不是 UML 规范的严格等级,不能据此认为所有组合关系都比聚合关系耦合更强。继承与实现是类型之间的关系(而非对象间关系),不参与上述强度排序。

常用画法 ​

txt
// 策略模式
┌──────────┐      ┌──────────┐
│ Context  │──────│ Strategy │◁─────┐
│          │      │ +execute()│      │
└──────────┘      └──────────┘  ┌───┴────┐
                                │ ImplA  │
                                │ ImplB  │
                                └────────┘

// 观察者模式
┌──────────┐      ┌──────────┐
│ Subject  │◇─────│ Observer │
│ +attach()│      │ +update()│
│ +notify()│      └──────────┘
└──────────┘            △
                   ┌────┴────┐
                   │Concrete │
                   │Observer │
                   └─────────┘

Released under the MIT License.