Skip to content
百科百科
操作系统
设计模式
算法
题解
java
leetcode
  • 设计模式
    • /设计模式/设计模式 - 单例.md
      • /设计模式/设计模式 - 中介者.md
        • /设计模式/设计模式 - 享元.md
          • 享元(Flyweight)
            • Intent
              • Class Diagram
                • Implementation
                  • JDK
                • /设计模式/设计模式 - 代理.md
                  • /设计模式/设计模式 - 原型模式.md
                    • /设计模式/设计模式 - 命令.md
                      • /设计模式/设计模式 - 备忘录.md
                        • /设计模式/设计模式 - 外观.md
                          • /设计模式/设计模式 - 工厂方法.md
                            • /设计模式/设计模式 - 抽象工厂.md
                              • /设计模式/设计模式 - 桥接.md
                                • /设计模式/设计模式 - 模板方法.md
                                  • /设计模式/设计模式 - 状态.md
                                    • /设计模式/设计模式 - 生成器.md
                                      • /设计模式/设计模式 - 空对象.md
                                        • /设计模式/设计模式 - 策略.md
                                          • /设计模式/设计模式 - 简单工厂.md
                                            • /设计模式/设计模式 - 组合.md
                                              • /设计模式/设计模式 - 装饰.md
                                                • /设计模式/设计模式 - 观察者.md
                                                  • /设计模式/设计模式 - 解释器.md
                                                    • /设计模式/设计模式 - 访问者.md
                                                      • /设计模式/设计模式 - 责任链.md
                                                        • /设计模式/设计模式 - 迭代器.md
                                                          • /设计模式/设计模式 - 适配器.md
                                                            • 一、前言
                                                              • 一、概述
                                                                • 设计模式目录

                                                                  2022年5月21日小于 1 分钟

                                                                  此页内容
                                                                  • 享元(Flyweight)
                                                                    • Intent
                                                                    • Class Diagram
                                                                    • Implementation
                                                                    • JDK

                                                                  # 享元(Flyweight)

                                                                  # Intent

                                                                  利用共享的方式来支持大量细粒度的对象,这些对象一部分内部状态是相同的。

                                                                  # Class Diagram

                                                                  • Flyweight:享元对象
                                                                  • IntrinsicState:内部状态,享元对象共享内部状态
                                                                  • ExtrinsicState:外部状态,每个享元对象的外部状态不同

                                                                  img

                                                                  # Implementation

                                                                  public interface Flyweight {
                                                                      void doOperation(String extrinsicState);
                                                                  }
                                                                  
                                                                  public class ConcreteFlyweight implements Flyweight {
                                                                  
                                                                      private String intrinsicState;
                                                                  
                                                                      public ConcreteFlyweight(String intrinsicState) {
                                                                          this.intrinsicState = intrinsicState;
                                                                      }
                                                                  
                                                                      @Override
                                                                      public void doOperation(String extrinsicState) {
                                                                          System.out.println("Object address: " + System.identityHashCode(this));
                                                                          System.out.println("IntrinsicState: " + intrinsicState);
                                                                          System.out.println("ExtrinsicState: " + extrinsicState);
                                                                      }
                                                                  }
                                                                  
                                                                  public class FlyweightFactory {
                                                                  
                                                                      private HashMap<String, Flyweight> flyweights = new HashMap<>();
                                                                  
                                                                      Flyweight getFlyweight(String intrinsicState) {
                                                                          if (!flyweights.containsKey(intrinsicState)) {
                                                                              Flyweight flyweight = new ConcreteFlyweight(intrinsicState);
                                                                              flyweights.put(intrinsicState, flyweight);
                                                                          }
                                                                          return flyweights.get(intrinsicState);
                                                                      }
                                                                  }
                                                                  
                                                                  public class Client {
                                                                  
                                                                      public static void main(String[] args) {
                                                                          FlyweightFactory factory = new FlyweightFactory();
                                                                          Flyweight flyweight1 = factory.getFlyweight("aa");
                                                                          Flyweight flyweight2 = factory.getFlyweight("aa");
                                                                          flyweight1.doOperation("x");
                                                                          flyweight2.doOperation("y");
                                                                      }
                                                                  }
                                                                  
                                                                  Object address: 1163157884
                                                                  IntrinsicState: aa
                                                                  ExtrinsicState: x
                                                                  Object address: 1163157884
                                                                  IntrinsicState: aa
                                                                  ExtrinsicState: y
                                                                  

                                                                  # JDK

                                                                  Java 利用缓存来加速大量小对象的访问时间。

                                                                  • java.lang.Integer#valueOf(int)
                                                                  • java.lang.Boolean#valueOf(boolean)
                                                                  • java.lang.Byte#valueOf(byte)
                                                                  • java.lang.Character#valueOf(char)
                                                                  编辑此页open in new window
                                                                  上次编辑于: 2022/5/21 13:08:59
                                                                  贡献者: yzqdev
                                                                  上一页
                                                                  /设计模式/设计模式 - 中介者.md
                                                                  下一页
                                                                  /设计模式/设计模式 - 代理.md
                                                                  powered by vuepress-theme-home