• 享元模式


      享元模式:运用共享技术有效地支持大量细粒度的对象
      享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实力来表示数据。如果能发现这些实例处理几个参数外基本上都是相同的,有时就能够受大幅度地减少需要实例化的类的数量。如果能把那些参数移到类实例的外面,在方法调用时将它们传递进来,就可以通过共享大幅度地减少单个实例的数目。

      如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑使用;还有就是对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。

    UML图:

      

    1 //Flyweight类,他是所有具体享元类的超类或接口,通过这个接口,Flyweight可以接受并作用域外部状态
    2 public abstract class Flyweight {
    3     public abstract void operation(int extrinsicstate);
    4 }
    1 //ConcreteFlyweight是继承FlyWeight超类或实现Flyweight接口,并为内部状态增加存储空间
    2 public class ConcreteFlyweight extends Flyweight {
    3 
    4     @Override
    5     public void operation(int extrinsicstate) {
    6         System.out.println("具体Flyweight:"+extrinsicstate);
    7     }
    8 
    9 }
     1 /**
     2  * UnsharedConcreteFlyweight是指那些不需要共享的Flyweight子类。
     3  *     因为Flyweight接口共享成为可能,但它并不强制共享。
     4  */
     5 
     6 public class UnsharedConcreteFlyweight extends Flyweight {
     7 
     8     @Override
     9     public void operation(int extrinsicstate) {
    10         System.out.println("不共享的具体Flyweight:"+extrinsicstate);
    11     }
    12 
    13 }
     1 /**
     2  * FlyweightFactory是一个享元工厂,用来创建并管理Flyweight对象。
     3  *     它主要是用来确保合理地共享Flyweight,当用户请求一个Flyweight时,
     4  *         FlyweightFactory对象提供一个以创建的实例或者创建一个(如果不存在的话)
     5  * @author 贤元
     6  *
     7  */
     8 public class FlyweightFactory {
     9     private Hashtable flyweights = new Hashtable();
    10     
    11     //初始化工厂时,先创建3个实例
    12     public FlyweightFactory(){
    13         flyweights.put("X", new ConcreteFlyweight());
    14         flyweights.put("Y", new ConcreteFlyweight());
    15         flyweights.put("Z", new ConcreteFlyweight());
    16     }
    17     //根据客户端请求,获得已生成的实例
    18     public Flyweight getFlyweight(String key){
    19         return (Flyweight) flyweights.get(key);
    20     }
    21 }
     1 public class TestClient {
     2     public static void main(String[] args) {
     3         //代码外部状态
     4         int extrinsicstate = 22;
     5         
     6         FlyweightFactory f = new FlyweightFactory();
     7         
     8         Flyweight fx = f.getFlyweight("X");
     9         fx.operation(--extrinsicstate);
    10         
    11         Flyweight fy = f.getFlyweight("Y");
    12         fx.operation(--extrinsicstate);
    13         
    14         Flyweight fz = f.getFlyweight("Z");
    15         fx.operation(--extrinsicstate);
    16         
    17         Flyweight uf = new UnsharedConcreteFlyweight();
    18         uf.operation(--extrinsicstate);
    19         
    20         /**打印结果:
    21          *  具体Flyweight:21
    22             具体Flyweight:20
    23             具体Flyweight:19
    24             不共享的具体Flyweight:18
    25          */
    26         
    27     }
    28 }
  • 相关阅读:
    CentOS /RHEL系统怎么更新安全补丁
    使用yum查询系统安装的软件及可以更新的软件并单独指定升级某一个软件
    惠普服务器通过ILO系统远程安装系统
    django进行数据库迁移的错误处理方法:You are trying to change the nullable field 'title' on book to non-nullable without a d
    Django数据库增删改查
    linux用户
    linux基础
    mysql小白系列_01 原理
    mysql运维入门6:MySQL读写分离
    mysql运维入门5:MySQL+kepalived高可用架构
  • 原文地址:https://www.cnblogs.com/lixianyuan-org/p/9538146.html
Copyright © 2020-2023  润新知