Java特性之设计模式【享元模式】

发布于:2024-05-07 ⋅ 阅读:(24) ⋅ 点赞:(0)

一、享元模式

概述

享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式

享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象

主要解决

在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重新创建

何时使用

1、系统中有大量对象

2、这些对象消耗大量内存

3、这些对象的状态大部分可以外部化

4、这些对象可以按照内蕴状态分为很多组,当把外蕴对象从对象中剔除出来时,每一组对象都可以用一个对象来代替

5、系统不依赖于这些对象身份,这些对象是不可分辨的

优缺点

优点:

  • 大大减少对象的创建,降低系统的内存,使效率提高

缺点:

  • 提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱

1. 各个角色介绍

1.1 享元工厂(Flyweight Factory)

  • 负责创建和管理享元对象,通常包含一个池(缓存)用于存储和复用已经创建的享元对象

1.2 具体享元(Concrete Flyweight)

  • 实现了抽象享元接口,包含了内部状态和外部状态。内部状态是可以被共享的,而外部状态则由客户端传递

1.3 抽象享元(Flyweight)

  • 定义了具体享元和非共享享元的接口,通常包含了设置外部状态的方法

2. UML图

​ 我们将创建一个 Shape 接口和实现了 Shape 接口的实体类 Circle。下一步是定义工厂类 ShapeFactory

ShapeFactory 有一个 CircleHashMap,其中键名为 Circle 对象的颜色。无论何时接收到请求,都会创建一个特定颜色的圆。ShapeFactory 检查它的 HashMap 中的 Circle 对象,如果找到 Circle 对象,则返回该对象,否则将创建一个存储在 HashMap 中以备后续使用的新对象,并把该对象返回到客户端

Main 类使用 ShapeFactory 来获取 Shape 对象。它将向 ShapeFactory 传递信息(red / green / blue/ black / white),以便获取它所需对象的颜色

在这里插入图片描述

3. 具体例子和代码

角色分配

  • Shape:形状接口
    • Circle:实现形状接口的圆形类
  • ShapeFactory:形状工厂

3.1 抽象形状及其实现类

  • Shape
package com.vinjcent.prototype.decorator;

/**
 * @author vinjcent
 * @description 形状接口
 * @since 2024/3/15 16:23
 */
public interface Shape {

    /**
     * 形状绘制动作
     */
    void draw();

}

  • Circle
package com.vinjcent.prototype.flyweight;

import io.swagger.annotations.ApiModelProperty;

/**
 * @author vinjcent
 * @description 圆形
 * @since 2024/3/20 20:31
 */
public class Circle implements Shape {

    @ApiModelProperty("颜色")
    private String color;

    @ApiModelProperty("x轴")
    private int x;

    @ApiModelProperty("y轴")
    private int y;

    @ApiModelProperty("半径")
    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);
    }

}

3.2 形状工厂

  • ShapeFactory
package com.vinjcent.prototype.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @author vinjcent
 * @description 形状工厂
 * @since 2024/3/22 17:33
 */
public class ShapeFactory {

    private static final Map<String, Shape> CIRCLE_MAP = new HashMap<>();

    public static Shape getCircle(String color) {

        // 根据color获取内存Map中的Shape对象
        Circle circle = (Circle) CIRCLE_MAP.get(color);

        // 如果不存在则创建一个Shape,并将该Shape存入缓存中
        if (circle == null) {
            circle = new Circle(color);
            CIRCLE_MAP.put(color, circle);
            System.out.println("The color of circle doesn't exist, Creating circle : " + color);
        }
        return circle;
    }

}

3.3 测试主函数

package com.vinjcent.prototype.flyweight;

/**
 * @author vinjcent
 * @description 享元模式
 * @since 2024/3/22 17:39
 */
public class Main {

    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() {
        // 生成一个[0, 5)区间的数
        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);
    }

}

  • 测试结果

在这里插入图片描述

4. 使用场景

  • 系统有大量相似对象
  • 需要缓冲池的场景

在这里插入图片描述