• 设计模式11 享元模式


    享元模式定义:运用共享技术来有効地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类的开销,从而提高系统资源的利用率。享元可以理解为“共享元件”。

    享元模式的主要优点是:

    相同对象只要保存一份,这降低了系统中对象的数量,从而降低了系统中细粒度对象给内存带来的压力。

    共享元件:

     1 public interface Flyweight {
     2     public void operation(UnsharableFlyweight unsharableFlyweight);
     3 }
     4 
     5 public class ConcreteFlyweightA implements Flyweight {
     6     private String key;
     7 
     8     public ConcreteFlyweightA(String key) {
     9         this.key = key;
    10     }
    11 
    12     @Override
    13     public void operation(UnsharableFlyweight unsharableFlyweight) {
    14         System.out.print("具体享元" + key + "被调用,");
    15         System.out.println("非享元信息是:" + unsharableFlyweight.getInfo());
    16     }
    17 }
    18 
    19 public class ConcreteFlyweightB implements Flyweight {
    20     private String key;
    21 
    22     public ConcreteFlyweightB(String key) {
    23         this.key = key;
    24     }
    25     @Override
    26     public void operation(UnsharableFlyweight unsharableFlyweight) {
    27         // TODO Auto-generated method stub
    28         System.out.print("具体享元" + key + "被调用,");
    29         System.out.println("非享元信息是:" + unsharableFlyweight.getInfo());
    30     }
    31 }

    非共享元件:

     1 public class UnsharableFlyweight {
     2     private String info;
     3 
     4     public UnsharableFlyweight(String info) {
     5         this.info = info;
     6     }
     7 
     8     public String getInfo() {
     9         return info;
    10     }
    11 
    12     public void setInfo(String info) {
    13         this.info = info;
    14     }
    15 }

    元件工厂:

     1 public class FlyweightFactory {
     2     private HashMap<String, Flyweight> flyweights;
     3 
     4     public FlyweightFactory() {
     5         flyweights = new HashMap<String, Flyweight>();
     6         flyweights.put("A", null);
     7         flyweights.put("B", null);
     8     }
     9 
    10     public Flyweight getFlyweight(String key) {
    11         Flyweight flyweight = (Flyweight) flyweights.get(key);
    12         if (flyweight != null) {
    13             System.out.println("具体享元" + key + "已经存在,直接返回");
    14         } else {
    15             if (key == "A") {
    16                 System.out.println("具体享元" + key + "不存在,新生成A");
    17                 flyweight = new ConcreteFlyweightA(key);
    18                 flyweights.put(key, flyweight);
    19             } else {
    20                 System.out.println("具体享元" + key + "不存在,新生成B");
    21                 flyweight = new ConcreteFlyweightB(key);
    22                 flyweights.put(key, flyweight);
    23             }
    24         }
    25         return flyweight;
    26     }
    27 }

    调用方式:

     1 public class Client {
     2     //线程池,数据库连接池
     3     public static void main(String[] args) {
     4         FlyweightFactory factory = new FlyweightFactory();
     5         Flyweight f1 = factory.getFlyweight("A");
     6         f1.operation(new UnsharableFlyweight("第1次调用A"));
     7         System.out.println("----------------------");
     8         
     9         Flyweight f2 = factory.getFlyweight("A");
    10         f2.operation(new UnsharableFlyweight("第2次调用A"));
    11         System.out.println("----------------------");
    12         
    13         Flyweight f3 = factory.getFlyweight("A");
    14         f3.operation(new UnsharableFlyweight("第3次调用B"));
    15         System.out.println("----------------------");
    16         
    17         Flyweight f4 = factory.getFlyweight("B");
    18         f4.operation(new UnsharableFlyweight("第1次调用B"));
    19         System.out.println("----------------------");
    20         
    21         Flyweight f5 = factory.getFlyweight("B");
    22         f5.operation(new UnsharableFlyweight("第2次调用B"));
    23         System.out.println("----------------------");
    24     }
    25 }

    执行结果:

     享元模式一般使用哈希数据结构实现。

  • 相关阅读:
    第 2 章 第 3 题 数组旋转问题 平移算法实现1
    翻屏类 h5 适配方案:解决宽高自适应难题
    如何摆脱项目命名困难的尴尬局面
    网易和淘宝的rem方案剖析
    真实前端面试题目
    前端开发面试题总结之——JAVASCRIPT(三)
    前端开发面试题总结之——JAVASCRIPT(二)
    前端开发面试题总结之——HTML
    前端开发面试题总结之——CSS3
    前端开发面试题(一)
  • 原文地址:https://www.cnblogs.com/asenyang/p/12111053.html
Copyright © 2020-2023  润新知