享元模式采用一个共享来避免大量拥有相同内容对象的开销。
在《自动装箱和拆箱》一文中提到,JAVA虚拟机启动后将-128~127之间Integer对象缓存起来,这样每次在使用-128~127之间的Integer对象时不用频繁创建,而是直接去缓存池里取。如果缓存池里存在该数值,就直接取出来;如果不存在,则返回新的对象。这种将常用或公共的数据缓存起来反复复用的方式,就体现了享元模式的思想。
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
// 如果i在-128~high之间,就直接在缓存中取出i的Integer类型对象
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
// 否则就在堆内存中创建
return new Integer(i);
}
private static class IntegerCache { // 静态内部类,虚拟机启动即加载至内存
static final int low = -128; // 缓存下界,值不可变
static final int high;
static final Integer cache[];
static { // 静态语句块
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
// 通过解码integerCacheHighPropValue,而得到一个候选的上界值
int i = parseInt(integerCacheHighPropValue);
// 上界最小为127
i = Math.max(i, 127);
// 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h; // 上界确定,此时high默认一般是127
// 创建缓存块,注意缓存数组大小
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++); // -128到high值逐一分配到缓存数组
}
private IntegerCache() {} // 构造方法,不需要构造什么
}
享元模式的角色
抽象享元(Flyweight):一个接口或抽象类,以规定具体享元角色需要实现的方法;
具体享元(ConcreteFlyweight):实现抽象享元角色所规定出的接口;
享元工厂(FlyweightFactory):负责创建和管理享元对象,将具体享元对象存储在一个享元池中,享元池一般设计为一个存储“键值对”的集合。当一个客户端对象调用一个享元对象的时候,享元工厂角色会检查系统中是否已经有一个符合条件的享元对象。如果有了,享元工厂角色就应当提供这个已有的享元对象;如果系统中没有,享元工厂角色就应该创建一个合适的享元对象。
客户端:使用享元对象。
享元模式案例
抽象享元
public interface IFlyweight {
public void setId(int id);
}
具体享元
public class ConcreteFlyweight implements IFlyweight {
private String name;
public ConcreteFlyweight(String name) {
this.name = name;
}
@Override
public void setId(int id) {
System.out.println("id:" + id + ",name: " + name);
}
}
享元工厂和客户端
import java.util.HashMap;
import java.util.Map;
public class FlyweightFactory {
private Map<String, Object> nameMap = new HashMap<String, Object>();
public IFlyweight getInstance(String name) {
IFlyweight flyweigth = (IFlyweight) nameMap.get(name);
if (flyweigth == null) {
flyweigth = new ConcreteFlyweight(name);
nameMap.put(name, flyweigth);
}
return flyweigth;
}
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
ConcreteFlyweight cfw1 = (ConcreteFlyweight) factory.getInstance("AO");
cfw1.setId(1);
ConcreteFlyweight cfw2 = (ConcreteFlyweight) factory.getInstance("USO");
cfw2.setId(2);
ConcreteFlyweight cfw3 = (ConcreteFlyweight) factory.getInstance("USO");
cfw3.setId(3);
System.out.println(factory.nameMap);
System.out.println(cfw1);
System.out.println(cfw2);
System.out.println(cfw3);
System.out.println(cfw1.equals(cfw2));
System.out.println(cfw2.equals(cfw3));
}
}
打印结果:
id:1,name: AO
id:2,name: USO
id:3,name: USO
{USO=com.niwodai.tech.item.flyweight.ConcreteFlyweight@1d1d565f, AO=com.niwodai.tech.item.flyweight.ConcreteFlyweight@711185e7}
com.niwodai.tech.item.flyweight.ConcreteFlyweight@711185e7
com.niwodai.tech.item.flyweight.ConcreteFlyweight@1d1d565f
com.niwodai.tech.item.flyweight.ConcreteFlyweight@1d1d565f
false
true