设计模式简述(十八)享元模式

享元模式

描述

当内存中存在大量类似的 对象时,可以考虑使用享元模式减少整体内存占用。

可以将相同的部分和不同的部分进行拆分,以达到多个对象共享相同部分内存的目的。

基本组件

通常享元对象通过共享的属性映射一个享元对象。

  • 公共对象
java 复制代码
public final class CommonData {
    private String province;
    private String city;
    private String county;

    public CommonData(String province, String city, String county) {
        this.province = province;
        this.city = city;
        this.county = county;
    }

    public CommonData() {
    }

    public synchronized void setData(String province, String city, String county) {
        this.province = province;
        this.city = city;
        this.county = county;
    }

    public String getProvince() {
        return province;
    }

    public String getCity() {
        return city;
    }

    public String getCounty() {
        return county;
    }

    @Override
    public String toString() {
        return "CommonData{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", county='" + county + '\'' +
                '}';
    }
}
  • 抽象享元对象(定义通用逻辑,持有公共对象)
java 复制代码
public abstract class AbstractFlyWeight {
    private final CommonData commonData;

    private String name;

    protected AbstractFlyWeight(CommonData commonData) {
        this.commonData = commonData;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProvince() {
        return commonData.getProvince();
    }

    public String getCity() {
        return commonData.getCity();
    }

    public String getCounty() {
        return commonData.getCounty();
    }

    public abstract void method();

    @Override
    public String toString() {
        return "AbstractFlyWeight{" +
                "commonData=" + commonData +
                ", name='" + name + '\'' +
                '}';
    }
}
  • 具体享元对象
java 复制代码
public class FlyWeight extends AbstractFlyWeight {
    protected FlyWeight(CommonData commonData) {
        super(commonData);
    }

    @Override
    public void method() {
        System.out.println("FlyWeight method ...");
    }
}
  • 享元对象工厂
java 复制代码
public class FlyWeightFactory {
    private static final Map<String, CommonData> map = new ConcurrentHashMap<>();

    public static CommonData getFlyWeight(String key) {
        return map.computeIfAbsent(key, s -> {
            String[] split = s.split("-");
            assert split.length == 3;
            return new CommonData(split[0], split[1], split[2]);
        });
    }
}

使用

创建多个对象共用相同key的公共对象

java 复制代码
public class Sample {
    public static void main(String[] args) {
        String key = "重庆市-渝北区-渝北区";
        CommonData commonData = FlyWeightFactory.getFlyWeight(key);
        AbstractFlyWeight flyWeight = new FlyWeight(commonData);
        flyWeight.setName("张三");
        flyWeight.method();
        System.out.println(flyWeight);
        AbstractFlyWeight flyWeight1 = new FlyWeight(commonData);
        flyWeight1.setName("李四");
        flyWeight1.method();
        System.out.println(flyWeight1);
    }
}
相关推荐
xiaolyuh1238 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
GISer_Jing18 小时前
智能体工具使用、规划模式
人工智能·设计模式·prompt·aigc
GISer_Jing19 小时前
AI Agent:学习与适应、模型上下文协议
人工智能·学习·设计模式·aigc
小马爱打代码20 小时前
MyBatis设计模式:构建者、工厂、代理模式
设计模式·mybatis·代理模式
月明长歌20 小时前
Javasynchronized 原理拆解:锁升级链路 + JVM 优化 + CAS 与 ABA 问题(完整整合版)
java·开发语言·jvm·安全·设计模式
会员果汁21 小时前
12.设计模式-状态模式
设计模式·状态模式
Yu_Lijing21 小时前
基于C++的《Head First设计模式》笔记——抽象工厂模式
c++·笔记·设计模式
会员果汁1 天前
13.设计模式-适配器模式
设计模式·适配器模式
GISer_Jing2 天前
AI:多智能体协作与记忆管理
人工智能·设计模式·aigc
雨中飘荡的记忆2 天前
责任链模式实战应用:从理论到生产实践
设计模式