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

享元模式

描述

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

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

基本组件

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

  • 公共对象
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);
    }
}
相关推荐
秋名RG6 小时前
深入理解设计模式之原型模式(Prototype Pattern)
设计模式·原型模式
Li小李同学Li20 小时前
设计模式【cpp实现版本】
单例模式·设计模式
周努力.1 天前
设计模式之状态模式
设计模式·状态模式
268572591 天前
Java 23种设计模式 - 行为型模式11种
java·开发语言·设计模式
摘星编程1 天前
并发设计模式实战系列(19):监视器(Monitor)
设计模式·并发编程
yangyang_z1 天前
【C++设计模式之Strategy策略模式】
c++·设计模式·策略模式
-qOVOp-1 天前
zst-2001 历年真题 设计模式
java·算法·设计模式
碎梦归途2 天前
23种设计模式-行为型模式之模板方法模式(Java版本)
java·开发语言·jvm·设计模式·软考·模板方法模式·软件设计师
YoseZang2 天前
【设计模式】GoF设计模式之策略模式(Strategy Pattern)
设计模式·策略模式