1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public final class FlyWeightFactory {
private static final FlyWeightFactory FACTORY = new FlyWeightFactory();
private final ConcurrentHashMap<String,SoftReference<FlyWeight>> cache = new ConcurrentHashMap<>();
private FlyWeightFactory() {
}
public static FlyWeightFactory factory() { return FACTORY; }
public FlyWeight getFlyWeight(String key){
if(cache.get(key) != null){ return cache.get(key).get();
}else{ FlyWeight flyWeight = new ConcreteFlyWeight(key); cache.putIfAbsent(key,new SoftReference<>(flyWeight)); return flyWeight; } } }
|
近期评论