设计模式-享元模式

设计模式-享元模式

享元模式(Flyweight Pattern)是一种结构型设计模式,主要用于减少创建大量相似对象对内存资源的消耗,通过共享这些对象来提高程序性能和系统资源利用率。在实际应用场景中string就是使用了享元模式,string a = “123”; string b = “123”;

我们假设有一个场景是需要创建大量的颜色对象,但其实大部分颜色对象的RGB值是重复的,因此可以通过享元模式来共享这些重复的颜色对象,以节省内存空间。

代码示例

/**
 * 颜色接口
 */
public interface Color {
   
    void paint(ColorContext context);

    // 获取颜色的RGB值
    int getRGB();
}
/**
 * 具体颜色实现
 */
public class ConcreteColor implements Color{
   
    private final int rgb;

    public ConcreteColor(int rgb) {
   
        this.rgb = rgb;
    }

    @Override
    public void paint(ColorContext context) {
   
        System.out.println("RGB的值是: " + rgb);
        // 在实际场景中会使用rgb值来填充颜色等操作
    }

    @Override
    public int getRGB() {
   
        return rgb;
    }
}
/**
 * 颜色上下文,存储享元对象外部状态
 */
public class ColorContext {
   
    private Point position;

    public ColorContext(Point position) {
   
        this.position = position;
    }

    public Point getPosition() {
   
        return position;
    }
}
/**
 * 享元颜色工厂
 */
public class ColorFactory {
   
    private Map<Integer, Color> colorMap = new HashMap<>();

    public Color getColor(int rgb) {
   
        if (!colorMap.containsKey(rgb)) {
   
            colorMap.put(rgb, new ConcreteColor(rgb));
        }
        return colorMap.get(rgb);
    }
}
/**
 * 享元模式客户端
 */
public class FlyweightDemo {
   
    public static void main(String[] args) {
   
        ColorFactory factory = new ColorFactory();

        Point pos1 = new Point(0, 0);
        Point pos2 = new Point(10, 10);

        Color red1 = factory.getColor(001);
        red1.paint(new ColorContext(pos1));

        // 同样的RGB值,复用同一个对象
        Color red2 = factory.getColor(001);
        red2.paint(new ColorContext(pos2));
        
        if (red1 == red2){
   
            System.out.println("对象示例相同");
        }

    }
}

打印结果:

Connected to the target VM, address: '127.0.0.1:49072', transport: 'socket'
RGB的值是: 1
RGB的值是: 1
对象示例相同
Disconnected from the target VM, address: '127.0.0.1:49072', transport: 'socket'

相关推荐

  1. 设计模式

    2024-01-24 15:48:01       34 阅读
  2. 设计模式-模式

    2024-01-24 15:48:01       32 阅读
  3. 设计模式模式

    2024-01-24 15:48:01       16 阅读
  4. 设计模式模式

    2024-01-24 15:48:01       14 阅读
  5. 【前端设计模式】之模式

    2024-01-24 15:48:01       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-24 15:48:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-24 15:48:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-24 15:48:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-24 15:48:01       20 阅读

热门阅读

  1. AI存在信任问题,区块链能提供帮助吗?

    2024-01-24 15:48:01       31 阅读
  2. spring和springboot的区别

    2024-01-24 15:48:01       42 阅读
  3. Spring Boot Banner 教程:自定义启动画面的艺术

    2024-01-24 15:48:01       42 阅读
  4. 23种设计模式概述

    2024-01-24 15:48:01       33 阅读
  5. 阿里云PTS如何使用?

    2024-01-24 15:48:01       44 阅读
  6. 矩阵上运算

    2024-01-24 15:48:01       35 阅读
  7. vue3.0规范学习记录

    2024-01-24 15:48:01       34 阅读
  8. FPGA 时钟资源

    2024-01-24 15:48:01       35 阅读
  9. MySQL 随机查询10条数据

    2024-01-24 15:48:01       29 阅读
  10. element-ui 打包流程源码解析——babel 相关

    2024-01-24 15:48:01       37 阅读
  11. Ubuntu 18.04 dns掉配置的问题解决

    2024-01-24 15:48:01       37 阅读