享元模式(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属性,它是内部状态并且可以共享,其他属性如xyradius是外部状态,由客户端设置。
  • ShapeFactory类 :是享元工厂类,负责创建和管理享元对象。它通过检查已存在的对象来确保相同颜色的Circle对象只创建一次,并在后续使用时进行共享。
  • FlyweightPatternDemo:是客户端代码,模拟了创建和绘制多个圆形对象的场景。通过享元工厂获取Circle对象,并设置外部状态进行绘制。

总结

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

相关推荐
ysy164806723935 分钟前
03算法学习_977、有序数组的平方
学习·算法
codists1 小时前
《算法导论(第4版)》阅读笔记:p83-p85
算法
caihuayuan51 小时前
生产模式下react项目报错minified react error #130的问题
java·大数据·spring boot·后端·课程设计
编程、小哥哥1 小时前
Java大厂面试:从Web框架到微服务技术的场景化提问与解析
java·spring boot·微服务·面试·技术栈·数据库设计·分布式系统
界面开发小八哥1 小时前
「Java EE开发指南」如何使用MyEclipse的可视化JSF编辑器设计JSP?(二)
java·ide·人工智能·java-ee·myeclipse
Tiny番茄1 小时前
归一化函数 & 激活函数
人工智能·算法·机器学习
jz_ddk2 小时前
[学习] RTKLib详解:rtcm2.c、rtcm3.c、rtcm3e与rtcmn.c
c语言·学习·算法
小学生的信奥之路2 小时前
力扣1991:找到数组的中间位置(前缀和)
数据结构·算法·leetcode·前缀和·数组
এ᭄画画的北北2 小时前
力扣-102.二叉树的层序遍历
数据结构·算法·leetcode
ccLianLian2 小时前
数据结构·字典树
数据结构·算法