1700474934
1700474935
我们继续看享元工厂,如代码清单28-9所示。
1700474936
1700474937
代码清单28-9 享元工厂
1700474938
1700474939
public class FlyweightFactory{
1700474940
1700474941
//定义一个池容器
1700474942
1700474943
private static HashMap<String,Flyweight>pool=new HashMap<String,Flyweight>();
1700474944
1700474945
//享元工厂
1700474946
1700474947
public static Flyweight getFlyweight(String Extrinsic){
1700474948
1700474949
//需要返回的对象
1700474950
1700474951
Flyweight flyweight=null;
1700474952
1700474953
//在池中没有该对象
1700474954
1700474955
if(pool.containsKey(Extrinsic)){
1700474956
1700474957
flyweight=pool.get(Extrinsic);
1700474958
1700474959
}else{
1700474960
1700474961
//根据外部状态创建享元对象
1700474962
1700474963
flyweight=new ConcreteFlyweight1(Extrinsic);
1700474964
1700474965
//放置到池中
1700474966
1700474967
pool.put(Extrinsic,flyweight);
1700474968
1700474969
}
1700474970
1700474971
return flyweight;
1700474972
1700474973
}
1700474974
1700474975
}
1700474976
1700474977
1700474978
1700474979
1700474981
设计模式之禅 28.3 享元模式的应用
1700474982
1700474983
28.3.1 享元模式的优点和缺点
[
上一页 ]
[ :1.700474934e+09 ]
[
下一页 ]