每天一个设计模式之享元模式(Flyweight Pattern)

享元是共享对象的意思,用来解决重复对象的内存浪费的问题。缓冲池就是为了共享对象而出现的一种技术,常见的使用池技术的场景有String常量池、数据库连接池、缓冲池等。

一、UML类图

在这里插入图片描述

角色说明:

  1. FlyWeight,是抽象的享元角色,它是产品抽象类,同时定义出对象的外部状态和内部状态的接口或实现类;
  2. ConcreteFlyweight,是具体的享元角色,是具体的产品类,实现抽象角色定义相关业务;
  3. UnsharedConcreteFlyweight,是不可共享的角色,一般不会出现在享元工厂;存在的意义是为了不强制享元对象一定共享;
  4. FlyweightFactory,享元工厂类,用于构建一个池容器(集合),同时提供从池中获取对象的方法;

内部状态

  • 指对象共享出来的信息,存储在享元对象内部且不会随环境的改变而改变;

外部状态

  • 指对象得以依赖的一个标记 ,是随环境改变而改变的,不可共享的状态;

二、记忆方法

『享』共享,『元』对象;
典型的共享技术——对象池,典型的对象池——String常量池,数据库连接池;

三、代码示例

下面是享元接口和享元类(共享)

// 享元接口
public interface Website {
    void use(User user);
}

// 享元具体类
public class ConcreteWebsite implements Website {
    private String type;

    public ConcreteWebsite(String type) {
        this.type = type;
    }

    @Override
    public void use(User user) {
        System.out.println("网站的发布形式: " + type + ", 在使用中");
    }

}

下面是享元对象工厂

public class WebsiteFactory {
    private Map<String, Website> sitePool = new HashMap<>();
    // 根据网站的类型返回一个网站,如果没有就创建一个网站,并放到池中并返回
    public Website getWebsiteByType(String type) {
        if (!sitePool.containsKey(type))
            sitePool.put(type, new ConcreteWebsite(type));
        return sitePool.get(type);
    }

    // 获取网站类型总数
    public int getWebsiteCount() {
        return sitePool.size();
    }
}

下面是客户类

public class Client {
    public static void main(String[] args) {
        WebsiteFactory websiteFactory = new WebsiteFactory();
        Website site1 = websiteFactory.getWebsiteByType("新闻");
        site1.use(new User("Tom"));

        Website site2 = websiteFactory.getWebsiteByType("财经");
        site2.use(new User("Lucy"));

        Website site3 = websiteFactory.getWebsiteByType("财经");
        site3.use(new User("John"));

        Website site4 = websiteFactory.getWebsiteByType("财经");
        site4.use(new User("Baden"));
    }
}

运行结果如下

website release: news, in using
website release: finance, in using
website release: finance, in using
website release: finance, in using
All category in websites sum up to => 2 kinds

Process finished with exit code 0

【参考】

  1. https://www.runoob.com/design-pattern/flyweight-pattern.html
  2. 韩顺平《设计模式》

设计模式系列博文导航

一、创建型 - 5种

原型模式(Prototype Pattern)
抽象工厂模式(Abstract Factory Pattern)
建造者模式(Builder Pattern)
工厂模式(Factory Pattern)
单例模式(Singleton Pattern)

助记语:原抽建工单

二、结构型 - 8种

享元模式(Flyweight Pattern)
代理模式(Proxy Pattern)
适配器模式(Adapter Pattern)
外观模式(Facade Pattern)

过滤器模式(Filter/Criteria Pattern)
桥接模式(Bridge Pattern)
组合模式(Composite Pattern)
装饰器模式(Decorator Pattern)

助记语:想呆室外,过桥组装

三、行为型 - 11种

责任链模式(Chain of Responsibility Pattern)
命令模式(Command Pattern)
解释器模式(Interpreter Pattern)
中介者模式(Mediator Pattern)
迭代器模式(Iterator Pattern)

观察者模式(Observer Pattern)
策略模式(Strategy Pattern)
状态模式(State Pattern)

备忘录模式(Memento Pattern)
模板方法模式(Template Pattern)
访问者模式(Visitor Pattern)

助记语:责令解中谍,观测状被模仿

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值