设计模式之享元模式(Flyweight)

设计模式之享元模式(Flyweight)

本篇为https://github.com/iluwatar/java-design-patterns/tree/master/flyweight阅读笔记

场景

炼金术士的商店里摆满了魔法药水。许多药水是相同的,因此不需要为每个药水创建新的对象。相反,一个对象实例可以表示多个货架项目,因此内存占用量仍然很小
它用于通过尽可能多地与类似对象共享来最小化内存使用或计算开销。

药水接口

public interface Potion {

    void drink();

}

解药

public class HealPotion implements Potion {
    @Override
    public void drink() {
        System.out.println("drink heal potion. @" + System.identityHashCode(this));
    }
}

毒药

public class PoisonPotion implements Potion {
    @Override
    public void drink() {
        System.out.println("drink poison potion. @" + System.identityHashCode(this));
    }
}

圣水

public class HolyWaterPotion implements Potion {
    @Override
    public void drink() {
        System.out.println("drink holy water potion. @" + System.identityHashCode(this));
    }
}

药厂

public class PotionFactory {


    enum PotionType {
        POISON, HEAL, HOLY_WATER;
    }

    public PotionFactory() {
        this.potions = new EnumMap<>(PotionType.class);
    }

    private final Map<PotionType, Potion> potions;

    public Potion createPotion(PotionType type) {
        Potion potion = potions.get(type);
        if (potion == null) {
            switch (type) {
                case HEAL:
                    potion = new HealPotion();
                    potions.put(type,potion);
                    break;
                case POISON:
                    potion = new PoisonPotion();
                    potions.put(type,potion);
                    break;
                case HOLY_WATER:
                    potion = new HolyWaterPotion();
                    potions.put(type,potion);
                    break;
                default:
                    break;
            }
        }
        return potion;

    }


}

药房

public class Shop {

    private List<Potion> topShelf;
    private List<Potion> bottomShelf;

    public Shop() {
        topShelf = new ArrayList<>();
        bottomShelf = new ArrayList<>();
        fillShelf();
    }

    private void fillShelf() {
        PotionFactory factory = new PotionFactory();

        topShelf.add(factory.createPotion(PotionFactory.PotionType.HEAL));
        topShelf.add(factory.createPotion(PotionFactory.PotionType.HEAL));
        topShelf.add(factory.createPotion(PotionFactory.PotionType.POISON));
        topShelf.add(factory.createPotion(PotionFactory.PotionType.POISON));
        topShelf.add(factory.createPotion(PotionFactory.PotionType.HOLY_WATER));
        topShelf.add(factory.createPotion(PotionFactory.PotionType.HOLY_WATER));

        bottomShelf.add(factory.createPotion(PotionFactory.PotionType.HEAL));
        bottomShelf.add(factory.createPotion(PotionFactory.PotionType.HEAL));
        bottomShelf.add(factory.createPotion(PotionFactory.PotionType.POISON));
        bottomShelf.add(factory.createPotion(PotionFactory.PotionType.POISON));
        bottomShelf.add(factory.createPotion(PotionFactory.PotionType.HOLY_WATER));
        bottomShelf.add(factory.createPotion(PotionFactory.PotionType.HOLY_WATER));
    }

    public void enumerate() {
        System.out.println("===top===");
        for (Potion potion : topShelf) {
            potion.drink();
        }

        System.out.println("===bottom===");
        for (Potion potion : bottomShelf) {
            potion.drink();
        }
    }

}

测试

public class App {

    @Test
    public void flyweightTest(){
        Shop shop = new Shop();
        shop.enumerate();
    }
}

输出

===top===
drink heal potion. @399573350
drink heal potion. @399573350
drink poison potion. @463345942
drink poison potion. @463345942
drink holy water potion. @195600860
drink holy water potion. @195600860
===bottom===
drink heal potion. @399573350
drink heal potion. @399573350
drink poison potion. @463345942
drink poison potion. @463345942
drink holy water potion. @195600860
drink holy water potion. @195600860

可以看到一共创建了三个对象

类图

flyweight是一个通过与其他类似对象共享尽可能多的数据来最小化内存使用的对象; 当简单的重复表示使用不可接受的内存量时,它是一种大量使用对象的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值