/** * PotionFactory is the Flyweight in this example. It minimizes memory use by sharing object * instances. It holds a map of potion instances and new potions are created only when none of the * type already exists. 在此示例中,PotionFactory是Flyweight。 通过共享对象实例,可最大程度地减少内存使用。 它拥有药水实例的映射,并且仅在不存在任何药水类型时才创建新药水。 */ publicclassPotionFactory{
privatefinal Map<PotionType, Potion> potions;
publicPotionFactory(){ potions = new EnumMap<>(PotionType.class); }
Potion createPotion(PotionType type){ var potion = potions.get(type); if (potion == null) { switch (type) { case HEALING: potion = new HealingPotion(); potions.put(type, potion); break; case HOLY_WATER: potion = new HolyWaterPotion(); potions.put(type, potion); break; case INVISIBILITY: potion = new InvisibilityPotion(); potions.put(type, potion); break; case POISON: potion = new PoisonPotion(); potions.put(type, potion); break; case STRENGTH: potion = new StrengthPotion(); potions.put(type, potion); break; default: break; } } return potion; } }
/** * AlchemistShop holds potions on its shelves. It uses PotionFactory to provide the potions. AlchemistShop在架子上放着药水。 它使用PotionFactory提供药水。 */ publicclassAlchemistShop{
/** * Get a read-only list of all the items on the top shelf. * * @return The top shelf potions */ publicfinal List<Potion> getTopShelf(){ return List.copyOf(this.topShelf); }
/** * Get a read-only list of all the items on the bottom shelf. * * @return The bottom shelf potions */ publicfinal List<Potion> getBottomShelf(){ return List.copyOf(this.bottomShelf); }
/** * Flyweight pattern is useful when the program needs a huge amount of objects. It provides means to * decrease resource usage by sharing object instances. * * <p>In this example {@link AlchemistShop} has great amount of potions on its shelves. To fill the * shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents the Flyweight in this * example). Internally {@link PotionFactory} holds a map of the potions and lazily creates new ones * when requested. * * <p>To enable safe sharing, between clients and threads, Flyweight objects must be immutable. * Flyweight objects are by definition value objects. 当程序需要大量对象时,Flyweight模式非常有用。 它提供了通过共享对象实例来减少资源使用的方法。 <p>在此示例中,{@ link AlchemistShop}的货架上有大量药水。 为了填充货架,{@ link AlchemistShop}使用了{@link PotionFactory}(在此代表着Flyweight 例)。 内部{@link PotionFactory}拥有一张药水地图,并在需要时懒惰地创建新药水。 <p>要在客户端和线程之间实现安全共享,Flyweight对象必须是不可变的。 Flyweight对象是定义值对象。 */ publicclassApp{
/** * Program entry point. * * @param args command line args */ publicstaticvoidmain(String[] args){ var alchemistShop = new AlchemistShop(); alchemistShop.enumerate(); } }
static { // high value may be configured by property int h = 127; //这里可以在运行时设置虚拟机参数来确定h :-Djava.lang.Integer.IntegerCache.high=250 String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) {//用户设置了 int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127);//虽然设置了但是还是不能小于127 // 也不能超过最大值 h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } high = h;
cache = new Integer[(high - low) + 1]; int j = low; //循环将区间的数赋值给cache[]数组 for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); }