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

享元模式

描述

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

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

基本组件

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

  • 公共对象
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);
    }
}
相关推荐
范纹杉想快点毕业17 小时前
实战级ZYNQ中断状态机FIFO设计
java·开发语言·驱动开发·设计模式·架构·mfc
茂桑21 小时前
DDD领域驱动设计-基础设施层
设计模式·架构
小温冲冲1 天前
通俗且全面精讲工厂设计模式
设计模式
进击的小头1 天前
设计模式与C语言高级特性的结合
c语言·设计模式
小温冲冲1 天前
通俗且全面精讲单例设计模式
开发语言·javascript·设计模式
Vivienne_ChenW1 天前
DDD领域模型在项目中的实战
java·开发语言·后端·设计模式
sg_knight1 天前
原型模式(Prototype)
python·设计模式·开发·原型模式
短剑重铸之日1 天前
《设计模式》第九篇:三大类型之结构型模式
java·后端·设计模式·组合模式·代理模式·结构性模式
忧郁的Mr.Li1 天前
设计模式--单例模式
javascript·单例模式·设计模式
范纹杉想快点毕业1 天前
状态机设计模式与嵌入式系统开发完整指南
java·开发语言·网络·数据库·mongodb·设计模式·架构