设计模式之Flyweight
副标题#e#
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;
}
}
#p#副标题#e#
至此,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:
#p#分页标题#e#
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越多,空间节减也就越大。