结构型(三) - 享元模式

news/2024/7/24 12:51:47 标签: 享元模式, java, 开发语言

一、概念

享元模式(Flyweight Pattern):所谓“享元”,顾名思义就是被共享的单元。享元模式的意图是复用对象,节省内存,前提是享元对象是不可变对象。

优点:可以极大地减少内存中对象的数量,使得相同对象或相似对象在内存中只保存一份。

缺点:享元对象会一直被享元工厂类引用,不利于JVM互收,除非经过线上验证,利用享元模式真的可以大大节省内存,否则,就不要过度使用这个模式。

使用场景:我们在需要创建大量(例如10^5)的相似的对象时,使用享元模式,把不可变的对象单独抽出来,进行共享可以减少内存消耗。不仅仅相同对象可以设计成享元,对于相似对象,我们也可以将这些对象中相同的部分(字段),提取出来设计成享元,让这些大量相似对象引用这些享元。

二、实现

虽然我不玩游戏,但是这里举一个游戏的例子,一般比较火的游戏会有比较大的访问量。比如某一个游戏,游戏人物都可以选武器,游戏中的武器就那么几种,比如无影剑。所有玩家都可以选择这个武器,由于这个武器一旦上线一般不会改动,它内部的杀伤力等配置都是固定的,所以没必要为每一个玩家新建一个武器对象,这个武器就可以设置为全局唯一享元对象。

1、武器装备抽象类

java">public interface Weaponry {
    void releaseLethality();
}

2、武器装备无影剑类(这名起的真俗,凑合看吧)

java">public class ShadowlessSword implements Weaponry {
    private String name;
    private int lethality;
    
    public ShadowlessSword(String name, int lethality) {
        this.name = name;
        this.lethality = lethality;
    }

    @Override
    public void releaseLethality() {
        System.out.println("释放杀伤力: " + lethality);
    }
}

3、武器装备工厂,新建一个武器,共所有玩家共享。

java">public class WeaponryFactory {
    private static final Map<String, Weaponry> weaponries = new HashMap<>();
    static {
        weaponries.put("无影剑", new ShadowlessSword("无影剑", 100));
    }
    
    public static Weaponry getWeaponry(String name){
        return weaponries.get(name);
    }
}

4、玩家类

java">public class Gamers {
    private Weaponry weaponry;

    public Gamers(Weaponry weaponry) {
       this.weaponry = weaponry;
    }
    
    public void startUseWeaponry() {
        weaponry.releaseLethality();
    }

    public Weaponry getWeaponry(){
        return weaponry;
    }
}

5、测试类

java">public class Client {
    public static void main(String[] args) {
        Gamers gamers1 = new Gamers(WeaponryFactory.getWeaponry("无影剑"));
        gamers1.startUseWeaponry();

        Gamers gamers2 = new Gamers(WeaponryFactory.getWeaponry("无影剑"));
        gamers2.startUseWeaponry();
        System.out.println("武器是否一样:" + (gamers1.getWeaponry() == gamers2.getWeaponry()));
    }
}

6、运行结果
<a class=享元模式运行结果.png" />

三、享元模式在 Java 中的应用

1、在Integer中的使用,看下面代码,多么熟悉的面试题。答案输出是true和false。

java">Integer i1 = 56;
Integer i2 = 56;
Integer i3 = 129;
Integer i4 = 129;

System.out.println(i1 == i2);
System.out.println(i3 == i4);

分析:Integer 是一个对象,赋值的时候会自动装箱,变成对象。按理说i1i2是不同对象,应该不等才对,之所以结果相等是因为:Integer 用到了享元模式来复用对象。

  • 执行代码Integer i1 = 56; 底层会调用valueOf方法。
java">Integer i = Integer.valueOf(59);
  • valueOf方法代码如下,当输入的值在一定范围的时候,会直接从IntegerCache中获取。
java">public static Integer valueOf(int i) {
     if (i >= IntegerCache.low && i <= IntegerCache.high) 
        return IntegerCache.cache[i + (-IntegerCache.low)]; 
     return new Integer(i);
}
  • 这里的 IntegerCache 相当于生成享元对象的工厂类,提前创建了-128 - 127之前的整数对象,所以上面的结果一个是true,一个是false。56的对象是直接从IntegerCache 中获取到的并非新建对象。除了 Integer 类型之外,其他包装器类型,比如 LongShortByte 等,也都利用了享元模式来缓存 -128 到 127 之间的数据。
java">/**
 * Cache to support the object identity semantics of autoboxing for values between
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage.  The size of the cache
 * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 * During VM initialization, java.lang.Integer.IntegerCache.high property
 * may be set and saved in the private system properties in the
 * sun.misc.VM class.
 */
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    static {
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}
  • 这个最大阈值是可以改的,修改方法在IntegerCache代码上面的注释了有,也不用记。
java"> -XX:AutoBoxCacheMax=<size>
  • 如下三种声明方式,后两种都会从缓存种拿,第一种会新建一个对象,所以推荐使用后两种进行声明。
java">Integer a = new Integer(33);
Integer a = 33;
Integer a = Integer.valueOf(33);

2、在String中的使用。看下面代码,和上面一样,答案输出是truefalse

java">String s1 = "哈哈";
String s2 = "哈哈";
String s3 = new String("哈哈");

System.out.println(s1 == s2);
System.out.println(s1 == s3);

Integer 类中要共享的对象,是在类加载的时候,就集中一次性创建好的。但是,对于字符串来说,我们没法事先知道要共享哪些字符串常量,所以没办法事先创建好,只能在某个字符串常量第一次被用到的时候,存储到常量池中,当之后再用到的时候,直接引用常量池中已经存在的即可,就不需要再重新创建了。

参考文章:
极客时间《设计模式》(王争)


http://www.niftyadmin.cn/n/4958433.html

相关文章

ABAP 定义复杂的数据结构

最近有个需求是实现ABAP数据类型与JASON类型的转换。想要创建个ABAP的数据类型来接JASON类型是个挺麻烦的事。例如下面这个JASON数据&#xff0c;是个很简单的数据结构。但对ABAP来说有4层了&#xff0c;就有点复杂了。 不过ABAP的数据类型也是支持直接定义数据结构的嵌套的。如…

smiley-http-proxy-servlet 实现springboot 反向代理,项目鉴权,安全的引入第三方项目服务

背景&#xff1a; 项目初期 和硬件集成&#xff0c;实现了些功能服务&#xff0c;由于是局域网环境&#xff0c;安全问题当时都可以最小化无视。随着对接的服务越来越多&#xff0c;部分功能上云&#xff0c;此时就需要有一种手段可以控制到其他项目/接口的访问权限。 无疑 反向…

MyBatis-Plus快速开始[MyBatis-Plus系列] - 第482篇

悟纤&#xff1a;师傅&#xff0c;MyBatis-Plus被你介绍的这么神乎其乎&#xff0c;咱们还是来的点实际的吧。 师傅&#xff1a;那真是必须的&#xff0c;学习技术常用的一种方法&#xff0c;就是实践。 悟纤&#xff1a;贱贱更健康。 师傅&#xff1a;这… 师傅&#xff1a;…

Linux 多线程中执行fork的情况

一、普通多线程中执行fork的情况 1.多线程中没有执行fork的情况 代码如下&#xff1a; #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h> #include<string.h> #include<semaphore.h>void*fun(void* arg) …

docker启动配置hosts

docker run -d --name gateway --add-hostnacos:[ip] --add-hostrabbitmq:[ip] --net"host" gateway:01

Navicat里.sql文件转换到.db文件

1.在桌面创建一个xxx.db文件&#xff0c;在navicat中创建数据库的时候会用到 2.在navicat创建数据库 在 Navicat 的导航栏中&#xff0c;选择 "工具" -> "SQL 文件执行器"。 在 SQL 文件执行器中&#xff0c;单击 "打开" 按钮&#xff0c;选择…

Pytorch 手写数字识别-MINIST 数据集训练

CNN 前期文章我们分享了tensorflow 的手写数字识别的训练以及识别过程,有网友私信是否写一下pytorch训练识别过程,本期文章我们来分享一下pytorch的手写数字训练人工智能TensorFlow(十六)MNIST手写数字识别 说到图片识别就不得不提卷积神经网络,我们会在后期详细介绍,或者…

测试框架pytest教程(10)自定义命令行-pytest_addoption

pytest_addoption pytest_addoption是pytest插件系统中的一个钩子函数&#xff0c;用于向pytest添加自定义命令行选项。 在pytest中&#xff0c;可以使用命令行选项来控制测试的行为和配置。pytest_addoption钩子函数允许您在运行pytest时添加自定义的命令行选项&#xff0c;…