Update 设计模式 - 组合.md

小节标题改为中文,重新绘制类图,补充参考资料。
This commit is contained in:
CyC2018
2026-10-28 03:20:01 +08:00
parent 6154db1fcb
commit a9011c2588
2 changed files with 11 additions and 10 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

+11 -10
View File
@@ -1,19 +1,16 @@
## 组合(Composite)
### Intent
### 意图
将对象组合成树形结构来表示“整体/部分”层次关系,允许用户以相同的方式处理单独对象和组合对象。
### Class Diagram
### 类图
组件(Component)类是组合类(Composite)和叶子类(Leaf)的父类,可以把组合类看成是树的中间节点。
组合对象拥有一个或者多个组件对象,因此组合对象的操作可以委托给组件对象去处理,而组件对象可以是另一个组合对象或者叶子对象。
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/2b8bfd57-b4d1-4a75-bfb0-bcf1fba4014a.png"/> </div><br>
### Implementation
<div align="center"> <img src="pics/dp-composite.png" width="760px"/> </div><br>
### 实现
```java
public abstract class Component {
protected String name;
@@ -114,7 +111,7 @@ public class Client {
}
```
```html
```text
Composite:root
--left:1
--Composite:2
@@ -124,10 +121,14 @@ Composite:root
--left:3
```
### JDK
### JDK 中的应用
- javax.swing.JComponent#add(Component)
- java.awt.Container#add(Component)
- java.util.Map#putAll(Map)
- java.util.List#addAll(Collection)
- java.util.Set#addAll(Collection)
## 参考资料
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.