享元模式(Flyweight Pattern)

享元模式(Flyweight Pattern)是一种结构型设计模式,它通过共享大量细粒度对象来减少内存消耗。享元模式的核心思想是将多个相似的对象共享起来,从而避免对象的大量创建,节省内存资源。

场景说明

假设你正在开发一个绘图应用程序,用户可以在画布上绘制许多形状。每个形状都有相同的颜色和大小属性,但它们的位置不同。使用享元模式可以将相同属性的形状对象共享,从而减少内存消耗。

享元模式结构

  1. Flyweight(享元):定义了对象的内部状态,这些状态是可以共享的。
  2. ConcreteFlyweight(具体享元):实现Flyweight接口,并且将可共享的状态存储在内部。
  3. FlyweightFactory(享元工厂):负责创建和管理享元对象,确保享元对象的共享性。
  4. Client(客户端):维护对享元对象的引用,并且通过享元工厂来获取享元对象。

代码实现

下面是一个使用享元模式的Java示例,演示如何在绘图应用程序中使用享元模式来优化内存使用。

java 复制代码
import java.util.HashMap;
import java.util.Map;

// 享元接口
interface Shape {
    void draw();
}

// 具体享元类:圆形
class Circle implements Shape {
    private String color;
    private int x;
    private int y;
    private int radius;

    public Circle(String color) {
        this.color = color;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :" + radius);
    }
}

// 享元工厂类
class ShapeFactory {
    private static final Map<String, Shape> circleMap = new HashMap<>();

    public static Shape getCircle(String color) {
        Circle circle = (Circle) circleMap.get(color);

        if (circle == null) {
            circle = new Circle(color);
            circleMap.put(color, circle);
            System.out.println("Creating circle of color : " + color);
        }
        return circle;
    }
}

// 客户端代码
public class FlyweightPatternDemo {
    private static final String[] colors = {"Red", "Green", "Blue", "White", "Black"};

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
            circle.setX(getRandomX());
            circle.setY(getRandomY());
            circle.setRadius(100);
            circle.draw();
        }
    }

    private static String getRandomColor() {
        return colors[(int) (Math.random() * colors.length)];
    }

    private static int getRandomX() {
        return (int) (Math.random() * 100);
    }

    private static int getRandomY() {
        return (int) (Math.random() * 100);
    }
}

输出结果

plaintext 复制代码
Creating circle of color : Red
Circle: Draw() [Color : Red, x : 36, y :56, radius :100
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 23, y :34, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 45, y :66, radius :100
Creating circle of color : White
Circle: Draw() [Color : White, x : 88, y :96, radius :100
Creating circle of color : Black
Circle: Draw() [Color : Black, x : 73, y :29, radius :100
Circle: Draw() [Color : Red, x : 12, y :10, radius :100
Circle: Draw() [Color : Green, x : 87, y :53, radius :100
...

代码解释

  • Shape接口 :定义了draw()方法,所有具体享元类都要实现这个接口。
  • Circle类 :是具体享元类,实现了Shape接口。Circle类中包含了一个color属性,它是内部状态并且可以共享,其他属性如x、y、radius是外部状态,由客户端设置。
  • ShapeFactory类 :是享元工厂类,负责创建和管理享元对象。它通过检查已存在的对象来确保相同颜色的Circle对象只创建一次,并在后续使用时进行共享。
  • FlyweightPatternDemo:是客户端代码,模拟了创建和绘制多个圆形对象的场景。通过享元工厂获取Circle对象,并设置外部状态进行绘制。

总结

享元模式通过共享对象来减少内存消耗,特别适用于大量细粒度对象的场景。然而,在使用享元模式时,需要注意区分对象的内部状态(共享部分)和外部状态(不共享部分),以确保对象的正确性和一致性。

相关推荐
就叫年华吧丶6 分钟前
长文档点目录定位总是不准?一个 content-visibility + 平滑滚动引发的连环坑(Vue3 实战)
前端·javascript·算法·vue
FPGA信号处理13 分钟前
【信号检测与估计】第四节课:非高斯噪声下的 BLUE、极大似然与 EM 算法
人工智能·算法·机器学习
驭渊的小故事22 分钟前
SpringBoot 配置文件详解:properties 与 yml 从入门到实战
java·开发语言·笔记
Wx-bishekaifayuan24 分钟前
Springcloud中学在线学习平台58883-计算机课程设计、毕业设计
java·vue.js·spring boot·学习·spring·spring cloud·课程设计
一嘴一个橘子29 分钟前
java 中使用 lua 脚本
java
码智社38 分钟前
Spring Framework 全方位技术解析
java
.道阻且长.39 分钟前
C++ 11:可变参数模板
前端·c++·算法
wangqiaowq1 小时前
AST 安全求值
java·开发语言
Java_2017_csdn2 小时前
OFC、ERP、OMS、WMS、TMS 对比-详解
java
Doubbbbbbble云3 小时前
基于贪心算法的区间覆盖问题求解策略4
算法·贪心算法