博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java共享模式/享元模式(Flyweight模式)
阅读量:3725 次
发布时间:2019-05-22

本文共 5688 字,大约阅读时间需要 18 分钟。

Flyweight定义:避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类)。

为什么使用共享模式/享元模式

面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可能显得很庞大,比如,字处理软件,如果以每个文字都作为一个对象,几千个字,对象数就是几千,无疑耗费内存,那么我们还是要"求同存异",找出这些对象群的共同点,设计一个元类,封装可以被共享的类,另外,还有一些特性是取决于应用(context),是不可共享的,这也Flyweight中两个重要概念内部状态intrinsic和外部状态extrinsic之分。
说白点,就是先捏一个的原始模型,然后随着不同场合和环境,再产生各具特征的具体模型,很显然,在这里需要产生不同的新对象,所以Flyweight模式中常出现Factory模式。Flyweight的内部状态是用来共享的,Flyweight factory负责维护一个Flyweight pool(模式池)来存放内部状态的对象。
Flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度。应用场合很多:比如你要从一个数据库中读取一系列字符串,这些字符串中有许多是重复的,那么我们可以将这些字符串储存在Flyweight池(pool)中。

如何使用共享模式/享元模式

我们先从Flyweight抽象接口开始:
public interface Flyweight{
 public void operation( ExtrinsicState state );
}
//用于本模式的抽象数据类型(自行设计)
public interface ExtrinsicState { }
下面是接口的具体实现(ConcreteFlyweight),并为内部状态增加内存空间,ConcreteFlyweight必须是可共享的,它保存的任何状态都必须是内部(intrinsic),也就是说,ConcreteFlyweight必须和它的应用环境场合无关。
public class ConcreteFlyweight implements Flyweight {
 private IntrinsicState state;
 public void operation( ExtrinsicState state ){
   //具体操作
 }
}
当然,并不是所有的Flyweight具体实现子类都需要被共享的,所以还有另外一种不共享的ConcreteFlyweight:
public class UnsharedConcreteFlyweight implements Flyweight {
 public void operation( ExtrinsicState state ) { }
}
Flyweight factory负责维护一个Flyweight池(存放内部状态),当客户端请求一个共享Flyweight时,这个factory首先搜索池中是否已经有可适用的,如果有,factory只是简单返回送出这个对象,否则,创建一个新的对象,加入到池中,再返回送出这个对象池。
public class FlyweightFactory {
 //Flyweight pool
 private Hashtable flyweights = new Hashtable();
 public Flyweight getFlyweight( Object key ) {
  Flyweight flyweight = (Flyweight) flyweights.get(key);
  if( flyweight == null ) {
   //产生新的ConcreteFlyweight
   flyweight = new ConcreteFlyweight();
   flyweights.put( key, flyweight );
  }
   return flyweight;
 }
}
至此,Flyweight模式的基本框架已经就绪,我们看看如何调用:
FlyweightFactory factory = new FlyweightFactory();
Flyweight fly1 = factory.getFlyweight( "Fred" );
Flyweight fly2 = factory.getFlyweight( "Wilma" );
......
从调用上看,好象是个纯粹的Factory使用,但奥妙就在于Factory的内部设计上。

Flyweight模式在XML等数据源中应用

我们上面已经提到,当大量从数据源中读取字符串,其中肯定有重复的,那么我们使用Flyweight模式可以提高效率,以唱片CD为例,在一个XML文件中,存放了多个CD的资料。
每个CD有三个字段:
  1. 出片日期(year)
  2. 歌唱者姓名等信息(artist)
  3. 唱片曲目 (title)
其中,歌唱者姓名有可能重复,也就是说,可能有同一个演唱者的多个不同时期 不同曲目的CD。我们将"歌唱者姓名"作为可共享的ConcreteFlyweight.其他两个字段作为UnsharedConcreteFlyweight。
首先看看数据源XML文件的内容:
<?xml version="1.0"?>
<collection>
<cd>
<title>Another Green World</title>
<year>1978</year>
<artist>Eno, Brian</artist>
</cd>
<cd>
<title>Greatest Hits</title>
<year>1950</year>
<artist>Holiday, Billie</artist>
</cd>
<cd>
<title>Taking Tiger Mountain (by strategy)</title>
<year>1977</year>
<artist>Eno, Brian</artist>
</cd>
.......
</collection>
虽然上面举例CD只有3张,CD可看成是大量重复的小类,因为其中成分只有三个字段,而且有重复的(歌唱者姓名)。
CD就是类似上面接口 Flyweight:
public class CD {
 private String title;
 private int year;
 private Artist artist;
 public String getTitle() {return title;}
 public int getYear() {return year;}
 public Artist getArtist() {return artist;}
 public void setTitle(String t){title = t;}
 public void setYear(int y){year = y;}
 public void setArtist(Artist a){artist = a;}
}
将"歌唱者姓名"作为可共享的ConcreteFlyweight:
public class Artist {
 //内部状态
 private String name;
 // note that Artist is immutable.
 String getName(){return name;}
 Artist(String n){
     name = n;
    }
}
再看看Flyweight factory,专门用来制造上面的可共享的ConcreteFlyweight:Artist
public class ArtistFactory {
 Hashtable pool = new Hashtable();
 Artist getArtist(String key){
  Artist result;
  result = (Artist)pool.get(key);
  产生新的Artist
  if(result == null) {
   result = new Artist(key);
   pool.put(key,result);  
  }
  return result;
    }
}
当你有几千张甚至更多CD时,Flyweight模式将节省更多空间,共享的flyweight越多,空间节省也就越大。
享元模式
概述
运用共享技术有效地支持大量细粒度的对象。
 适用性
当都具备下列情况时,使用Flyweight模式:    1.一个应用程序使用了大量的对象。    2.完全由于使用大量的对象,造成很大的存储开销。    3.对象的大多数状态都可变为外部状态。    4.如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。    5.应用程序不依赖于对象标识。由于Flyweight对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。
 参与者
1.Flyweight      描述一个接口,通过这个接口flyweight可以接受并作用于外部状态。    2.ConcreteFlyweight      实现Flyweight接口,并为内部状态(如果有的话)增加存储空间。      ConcreteFlyweight对象必须是可共享的。它所存储的状态必须是内部的;即,它必须独立于ConcreteFlyweight对象的场景。    3.UnsharedConcreteFlyweight      并非所有的Flyweight子类都需要被共享。Flyweight接口使共享成为可能,但它并不强制共享。      在Flyweight对象结构的某些层次,UnsharedConcreteFlyweight对象通常将ConcreteFlyweight对象作为子节点。    4.FlyweightFactory      创建并管理flyweight对象。      确保合理地共享flyweight。当用户请求一个flyweight时,FlyweightFactory对象提供一个已创建的实例或者创建一个(如果不存在的话)。
 类图
 例子
Flyweight
public interface Flyweight {    void action(int arg);}
ConcreteFlyweight
public class FlyweightImpl implements Flyweight {    public void action(int arg) {        // TODO Auto-generated method stub        System.out.println("参数值: " + arg);    }}
FlyweightFactory
public class FlyweightFactory {    private static Map flyweights = new HashMap();        public FlyweightFactory(String arg) {        flyweights.put(arg, new FlyweightImpl());    }        public static Flyweight getFlyweight(String key) {        if (flyweights.get(key) == null) {            flyweights.put(key, new FlyweightImpl());        }        return flyweights.get(key);    }        public static int getSize() {        return flyweights.size();    }}
Test
public class Test {    public static void main(String[] args) {        // TODO Auto-generated method stub        Flyweight fly1 = FlyweightFactory.getFlyweight("a");        fly1.action(1);                Flyweight fly2 = FlyweightFactory.getFlyweight("a");        System.out.println(fly1 == fly2);                Flyweight fly3 = FlyweightFactory.getFlyweight("b");        fly3.action(2);                Flyweight fly4 = FlyweightFactory.getFlyweight("c");        fly4.action(3);                Flyweight fly5 = FlyweightFactory.getFlyweight("d");        fly4.action(4);                System.out.println(FlyweightFactory.getSize());    }}
result
参数值: 1true参数值: 2参数值: 3参数值: 44

转载地址:http://nopnn.baihongyu.com/

你可能感兴趣的文章
JAVA问题总结第一期
查看>>
Java中的“>”、“>>”、“>>>”三个符号的区别
查看>>
Int 转 byte 数组以及相关原理
查看>>
Java中字节Byte和位Bit的关系
查看>>
Java中的基本数据类型和引用数据类型的区别
查看>>
Java中的String,StringBuilder,StringBuffer三者的区别
查看>>
java中NULL与“ “的区别
查看>>
静态方法与实例化方法的常见误区
查看>>
C++基础入门
查看>>
C++核心编程
查看>>
C++编程提高
查看>>
阶段性总结问题整理
查看>>
Socket兼容IP和域名的原因
查看>>
设置软件的默认打开方式
查看>>
C++ Primer 阅读笔记(一)
查看>>
C++ Primer 阅读笔记(二)
查看>>
基于轮廓的连通域标记及应用(opencv及c++)
查看>>
opencv学习笔记一(imread,imshow,cvtColor,imwrite)
查看>>
opencv学习笔记二(矩阵的掩膜操作)
查看>>
opencv学习笔记三(Mat对像)
查看>>