合肥网站建设开发电话,网站建设报价图片欣赏,青岛快速排名优化,加强文明网站内容建设一、概念
享元模式#xff08;Flyweight Pattern#xff09;#xff1a;所谓“享元”#xff0c;顾名思义就是被共享的单元。享元模式的意图是复用对象#xff0c;节省内存#xff0c;前提是享元对象是不可变对象。
优点#xff1a;可以极大地减少内存中对象的数量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;}Overridepublic void releaseLethality() {System.out.println(释放杀伤力 lethality);}
}3、武器装备工厂新建一个武器共所有玩家共享。
public class WeaponryFactory {private static final MapString, 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、运行结果
三、享元模式在 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 是一个对象赋值的时候会自动装箱变成对象。按理说i1和i2是不同对象应该不等才对之所以结果相等是因为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 类型之外其他包装器类型比如 Long、Short、Byte 等也都利用了享元模式来缓存 -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:AutoBoxCacheMaxsize} 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 propertyint 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_VALUEh 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:AutoBoxCacheMaxsize如下三种声明方式后两种都会从缓存种拿第一种会新建一个对象所以推荐使用后两种进行声明。
Integer a new Integer(33);
Integer a 33;
Integer a Integer.valueOf(33);2、在String中的使用。看下面代码和上面一样答案输出是true和false。
String s1 哈哈;
String s2 哈哈;
String s3 new String(哈哈);System.out.println(s1 s2);
System.out.println(s1 s3);Integer 类中要共享的对象是在类加载的时候就集中一次性创建好的。但是对于字符串来说我们没法事先知道要共享哪些字符串常量所以没办法事先创建好只能在某个字符串常量第一次被用到的时候存储到常量池中当之后再用到的时候直接引用常量池中已经存在的即可就不需要再重新创建了。
参考文章 极客时间《设计模式》王争