当前位置: 首页 > news >正文

合肥网站建设开发电话网站建设报价图片欣赏

合肥网站建设开发电话,网站建设报价图片欣赏,青岛快速排名优化,加强文明网站内容建设一、概念 享元模式#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 类中要共享的对象是在类加载的时候就集中一次性创建好的。但是对于字符串来说我们没法事先知道要共享哪些字符串常量所以没办法事先创建好只能在某个字符串常量第一次被用到的时候存储到常量池中当之后再用到的时候直接引用常量池中已经存在的即可就不需要再重新创建了。 参考文章 极客时间《设计模式》王争
http://www.zqtcl.cn/news/605711/

相关文章:

  • 中国室内设计联盟网官网网站专题页优化
  • 设计模板图热狗网站关键词优化
  • 无锡网站开发公司重庆网站有哪些
  • 做网站找什么公司工作网站开发思维导图内容
  • 有人知道做网站吗?wordpress多站点cdn
  • 网站风格特点大型外包公司有哪些
  • 如何网站seo用asp做网站有哪控件
  • 网站建设需要哪些成本wordpress商城建站教程
  • 做网络的网站很重要吗网站认证费用
  • flash网站项目背景网页截图快捷键可拉动
  • 郑州企业建设网站北京企业网站模板建站开发
  • 宣传旅游网站建设的观点是什么公众号怎么推广和引流
  • 企业网站制作多少钱山西网络营销方案
  • 焦作住房和城乡建设局网站旅行网站模板
  • 男做基视频网站国家重点高新技术企业名单
  • 公司官方网站开发网站建设电子商务
  • seo网站优化系统搜索引擎优化排名案例
  • 郑州网站建设工作室网站建设全流程 知乎
  • 如何利用源码做网站外贸网站制作推广
  • 国内做网站哪家公司好免费查找资料的网站
  • 自己做的网站百度搜不到搭建网站seo
  • 奇墙网站建设高端网站建设公司联系电话
  • 宁波那家公司做网站好中企动力科技股份有限公司招聘
  • 水果网站推广网站首页静态好还是动态好
  • iis网站属性小程序源码无需服务器
  • 景区网站建设材料代运营有哪些套路坑
  • 六安电商网站建设哪家好有关做美食的网站
  • 卸载wordpress插件网店seo关键词
  • 金山网站制作赤城seo网站优化排名
  • 提供坪山网站建设深圳商城网站哪家做的好