Update 设计模式 - 享元.md

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

After

Width:  |  Height:  |  Size: 170 KiB

+11 -12
View File
@@ -1,21 +1,16 @@
## 享元(Flyweight)
### Intent
### 意图
利用共享的方式来支持大量细粒度的对象,这些对象一部分内部状态是相同的。
### Class Diagram
### 类图
- Flyweight:享元对象
- IntrinsicState:内部状态,享元对象共享内部状态
- ExtrinsicState:外部状态,每个享元对象的外部状态不同
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/5f5c22d5-9c0e-49e1-b5b0-6cc7032724d4.png"/> </div><br>
### Implementation
<div align="center"> <img src="pics/dp-flyweight.png" width="760px"/> </div><br>
### 实现
```java
public interface Flyweight {
void doOperation(String extrinsicState);
@@ -68,7 +63,7 @@ public class Client {
}
```
```html
```text
Object address: 1163157884
IntrinsicState: aa
ExtrinsicState: x
@@ -77,11 +72,15 @@ IntrinsicState: aa
ExtrinsicState: y
```
### JDK
### JDK 中的应用
Java 利用缓存来加速大量小对象的访问时间。
- java.lang.Integer#valueOf(int)
- java.lang.Boolean#valueOf(boolean)
- java.lang.Byte#valueOf(byte)
- java.lang.Character#valueOf(char)
## 参考资料
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.