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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一套被广泛应用于Java编程的经典设计思想和解决方案。它们帮助开发人员解决常见的软件设计问题,并提供了可重用的代码模板。下面是一些常见的Java设计模式: 1. 创建型模式(Creational Patterns): - 工厂模式(Factory Pattern):通过工厂方法创建对象,隐藏对象的创建逻辑。 - 抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定具体类。 - 单例模式(Singleton Pattern):确保一个类只有一个实例,并提供全局访问点。 - 建造者模式(Builder Pattern):将一个复杂对象的构建过程与其表示分离,使得同样的构建过程可以创建不同的表示。 - 原型模式(Prototype Pattern):通过复制现有对象来创建新对象,避免了使用new操作符。 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern):将一个类的接口转换成客户端所期望的另一个接口。 - 桥接模式(Bridge Pattern):将抽象部分与实现部分分离,使它们可以独立变化。 - 组合模式(Composite Pattern):将对象组合成树形结构以表示“部分-整体”的层次结构。 - 装饰器模式(Decorator Pattern):动态地给一个对象添加额外的职责。 - 外观模式(Facade Pattern):为子系统中的一组接口提供一个统一的接口。 - 享元模式(Flyweight Pattern):通过共享对象来减少内存使用,提高性能。 3. 行为型模式(Behavioral Patterns): - 模板方法模式(Template Method Pattern):定义一个算法的骨架,将一些步骤延迟到子类中实现。 - 策略模式(Strategy Pattern):定义一系列算法,将它们封装起来,并使它们可以互相替换。 - 观察者模式(Observer Pattern):定义对象间的一种一对多的依赖关系,当一个对象状态改变时,所有依赖它的对象都会收到通知。 - 迭代器模式(Iterator Pattern):提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部表示。 - 命令模式(Command Pattern):将请求封装成对象,使得可以用不同的请求对客户进行参数化。 - 责任链模式(Chain of Responsibility Pattern):将请求的发送者和接收者解耦,使多个对象都有机会处理这个请求。 - 状态模式(State Pattern):允许对象在内部状态改变时改变它的行为。 - 访问者模式(Visitor Pattern):在不改变对象结构的前提下,定义作用于对象结构中元素的新操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值