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

享元模式

描述

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

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

基本组件

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

  • 公共对象
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);
    }
}
相关推荐
yangpipi-16 小时前
2. 设计模式之结构型模式
设计模式
进击的小头20 小时前
设计模式组合应用:嵌入式通信协议栈
c语言·设计模式·策略模式
致Great21 小时前
智能体的设计模式探讨
设计模式
BD_Marathon1 天前
设计模式——单一职责原则
设计模式·单一职责原则
stevenzqzq1 天前
Slot API 设计模式
设计模式·compose
reddingtons1 天前
Cascadeur:动态总是“飘”?“物理外挂流” 3分钟直出重力感 2D 立绘
游戏·设计模式·aigc·设计师·游戏策划·游戏美术·cascadeur
Wyy_9527*1 天前
行为型设计模式——策略模式
设计模式·策略模式
kogorou0105-bit1 天前
前端设计模式:发布订阅与依赖倒置的解耦之道
前端·设计模式·面试·状态模式
BD_Marathon1 天前
设计模式——接口隔离原则
java·设计模式·接口隔离原则
小码过河.2 天前
设计模式——适配器模式
设计模式·适配器模式