java設計模式進階_flyweight

來源:互聯網
上載者:User

//////  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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.