结构型(三) - 享元模式

一、概念

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

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

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

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

二、实现

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

1、武器装备抽象类

public interface Weaponry {
    void releaseLethality();
}

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

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、武器装备工厂,新建一个武器,共所有玩家共享。

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、玩家类

public class Gamers {
    private Weaponry weaponry;

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

    public Weaponry getWeaponry(){
        return weaponry;
    }
}

5、测试类

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、运行结果
享元模式运行结果.png

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

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

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方法。
Integer i = Integer.valueOf(59);
  • valueOf方法代码如下,当输入的值在一定范围的时候,会直接从IntegerCache中获取。
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 之间的数据。
/**
 * 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代码上面的注释了有,也不用记。
 -XX:AutoBoxCacheMax=<size>
  • 如下三种声明方式,后两种都会从缓存种拿,第一种会新建一个对象,所以推荐使用后两种进行声明。
Integer a = new Integer(33);
Integer a = 33;
Integer a = Integer.valueOf(33);

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值