////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : Potion.java// @ Date : 2016/8/23// @ Author : /////* * 藥水 介面 */public interface Potion { public void drink();}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : HealingPotion.java// @ Date : 2016/8/23// @ Author : ////public class HealingPotion implements Potion { public void drink() { System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")"); }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : HolyWaterPotion.java// @ Date : 2016/8/23// @ Author : ////public class HolyWaterPotion implements Potion { public void drink() { System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")"); }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : InvisibilityPotion.java// @ Date : 2016/8/23// @ Author : ////public class InvisibilityPotion implements Potion { public void drink() { System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")"); }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : PoisonPotion.java// @ Date : 2016/8/23// @ Author : ////public class PoisonPotion implements Potion { public void drink() { System.out.println("Urgh!This is poisonous.(Potion=" + System.identityHashCode(this) + ")"); }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : StrengthPotion.java// @ Date : 2016/8/23// @ Author : ////public class StrengthPotion implements Potion { public void drink() { System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")"); }}/* * 枚舉藥水 */public enum PotionType { HEALING,//療傷藥水 INVISIBILITY,//隱形藥水 STRENGTH,//力量藥水 HOLY_WATER,//神聖的藥水 POISON; //有毒的藥水}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : PotionFactory.java// @ Date : 2016/8/23// @ Author : ////public class PotionFactory { private final Map<PotionType,Potion> potions; public PotionFactory(){ potions = new EnumMap<>(PotionType.class); } Potion createPotion(PotionType type) { Potion 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; }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : AlchemistShop.java// @ Date : 2016/8/23// @ Author : /////* * 鍊金術士商店 * AlchemistShop 持有藥水在它的藥架上 * 它使用藥水工廠去提供一些藥水. */public class AlchemistShop { private List<Potion> topShelf; private List<Potion> bottomShelf; public AlchemistShop() { topShelf = new ArrayList<>(); bottomShelf = new ArrayList<>(); fillShelves(); } private void fillShelves() { PotionFactory factory = new PotionFactory(); topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); topShelf.add(factory.createPotion(PotionType.STRENGTH)); topShelf.add(factory.createPotion(PotionType.HEALING)); topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); topShelf.add(factory.createPotion(PotionType.STRENGTH)); topShelf.add(factory.createPotion(PotionType.HEALING)); topShelf.add(factory.createPotion(PotionType.HEALING)); bottomShelf.add(factory.createPotion(PotionType.POISON)); bottomShelf.add(factory.createPotion(PotionType.POISON)); bottomShelf.add(factory.createPotion(PotionType.POISON)); bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER)); bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER)); } public void enumerate() { System.out.println("Enumerating top shelf potions\n"); for (Potion p : topShelf) { p.drink(); } System.out.println("\nEnumerating bottom shelf potions\n"); for (Potion p : bottomShelf) { p.drink(); } }}/* * 享元模式是有用的,當程式需要大量的對象。它提供了手段,減少資源使用通過共用對象執行個體。 * 在這個例子中AlchemistShop大量藥劑的貨架上。 * 填充貨架AlchemistShop使用PotionFactory(代表了輕量級選手在這個例子)。 * 內部PotionFactory藥水的地圖,懶載入地建立新的資源。 */public class App { public static void main(String[] args) { AlchemistShop as = new AlchemistShop(); as.enumerate(); }}//// Enumerating top shelf potions//// You become invisible. (Potion=916483725)// You become invisible. (Potion=916483725)// You feel strong. (Potion=1589249791)// You feel healed. (Potion=119635951)// You become invisible. (Potion=916483725)// You feel strong. (Potion=1589249791)// You feel healed. (Potion=119635951)// You feel healed. (Potion=119635951)//// Enumerating bottom shelf potions//// Urgh!This is poisonous.(Potion=676734865)// Urgh!This is poisonous.(Potion=676734865)// Urgh!This is poisonous.(Potion=676734865)// You feel blessed. (Potion=809481543)// You feel blessed. (Potion=809481543)