diff --git a/notes/pics/dp-flyweight.png b/notes/pics/dp-flyweight.png new file mode 100644 index 00000000..e41b8176 Binary files /dev/null and b/notes/pics/dp-flyweight.png differ diff --git a/notes/设计模式 - 享元.md b/notes/设计模式 - 享元.md index be8cc52b..bf29e556 100644 --- a/notes/设计模式 - 享元.md +++ b/notes/设计模式 - 享元.md @@ -1,21 +1,16 @@ ## 享元(Flyweight) -### Intent - - - +### 意图 利用共享的方式来支持大量细粒度的对象,这些对象一部分内部状态是相同的。 -### Class Diagram - +### 类图 - Flyweight:享元对象 - IntrinsicState:内部状态,享元对象共享内部状态 - ExtrinsicState:外部状态,每个享元对象的外部状态不同 -

- -### Implementation +

+### 实现 ```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.