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

享元模式

描述

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

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

基本组件

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

  • 公共对象
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);
    }
}
相关推荐
J_liaty17 小时前
23种设计模式一代理模式
设计模式·代理模式
苏渡苇1 天前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·设计模式·学习方法·责任链模式
短剑重铸之日1 天前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
feasibility.1 天前
AI 编程助手进阶指南:从 Claude Code 到 OpenCode 的工程化经验总结
人工智能·经验分享·设计模式·自动化·agi·skills·opencode
BD_Marathon1 天前
七大设计原则介绍
设计模式
YigAin1 天前
Unity23种设计模式之 享元模式
设计模式·享元模式
范纹杉想快点毕业2 天前
实战级ZYNQ中断状态机FIFO设计
java·开发语言·驱动开发·设计模式·架构·mfc
茂桑2 天前
DDD领域驱动设计-基础设施层
设计模式·架构
小温冲冲2 天前
通俗且全面精讲工厂设计模式
设计模式
进击的小头2 天前
设计模式与C语言高级特性的结合
c语言·设计模式