享元模式可以减少内存占用,重复数据不再开辟内存。

Integer的-128~127,String的装箱,我认为都可以算做享元模式。 

其核心(伪)代码如下:

  private static Map<T1,T2 > map= new HashMap<T1, T2>();
   
    private ConstructorMethod() {}
   
    public static method createObject(T1 t1) {
        T2 t2= map.get(t1);
        if (t2== null) {
            t2= new ConcurrentWebSite(t1);
            map.put(t1, t2);
        }
        return t2;
    }

在一个类中通过一个Map来保存数据,如果数据重复则不会创建。

相关文章:

  • 2021-06-18
猜你喜欢
  • 2021-10-02
  • 2022-12-23
  • 2021-06-13
  • 2021-07-01
  • 2021-12-11
  • 2021-09-24
  • 2022-12-23
相关资源
相似解决方案