java设计模式进阶_flyweight

这里写图片描述

//
//
//  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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值