ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计模式11-享元模式(Flyweight)

2021-12-01 23:34:18  阅读:147  来源: 互联网

标签:11 享元 code int static new Integer Circle 设计模式


设计模式11-享元模式(Flyweight)

概述

Use sharing to support large numbers of fine-grained objects efficiently.

享元模式(Flyweight Pattern):使用共享对象可有效地支持大量的细粒度的对象,从而节省内存。

简单实现

JDK实现

com.demo.design.pattern.structural.flyweight.Circle

@Data
public class Circle {
    private int radius;
    private Font font;

    public void draw() {
        System.out.println("Circle: [Font : " + font + ", radius : " + radius + "]");
    }
}

调用示例

@Test
public void flyWeightPattern(){
    // 事先定义好需要的Circle对象的共享属性
    Map<Integer, Font> fontMap = new HashMap<>(10);
    fontMap.put(1, new Font("Arial Bold", Font.PLAIN, 1));
    fontMap.put(2, new Font("Arial Bold", Font.BOLD, 2));
    
    List<Circle> circles = new ArrayList<>(100000);
    Circle circle;
    for (int i = 0; i < 100000; ++i) {
        circle = new Circle();
        // 需要大量Circle对象时,共享属性属性可以引用同一个对象
        circle.setFont(fontMap.get(i & 1));
        
        // 非共享属性则每个Circle独有
        circle.setRadius(new Random().nextInt(100));
        circles.add(circle);
    }
    circles.forEach(Circle::draw);
}

源码解析

JDK

java.lang.Integer

public final class Integer extends Number implements Comparable<Integer> {
    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
}

Integer缓存对象缓存了 -128 到 127 之间的整型值,这是最常用的一部分整型值,JDK也提供了方法来自定义缓存的最大值。

java.lang.Integer.IntegerCache

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() {}
}

优点

减少应用程序创建的对象, 降低程序内存的占用, 增强程序的性能。

缺点

提高了系统复杂性, 需要分离出外部状态和内部状态, 而且外部状态具有固化特性, 不应该随内部状态改变而改变, 否则导致系统的逻辑混乱。

使用场景

  • 系统中存在大量的相似对象。

  • 细粒度的对象都具备较接近的外部状态, 而且内部状态与环境无关, 也就是说对象没有特定身份。

  • 需要缓冲池的场景。

参考资料

标签:11,享元,code,int,static,new,Integer,Circle,设计模式
来源: https://www.cnblogs.com/xubighead/p/15631288.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有