享元模式(设计模式)

享元模式(Flyweight Pattern)是一种结构型设计模式,它通过共享细粒度对象来减少内存使用,从而提高性能。在享元模式中,多个对象可以共享相同的状态以减少内存消耗,特别适合用于大量相似对象的场景。

享元模式的核心思想

享元模式的核心思想是将对象的状态分为内部状态和外部状态:

● 内部状态:对象中可以共享的部分,不会随环境的改变而改变。

● 外部状态:对象中随环境改变而变化的部分,不能被共享。

通过将对象的内部状态和外部状态分离,可以使多个对象共享相同的内部状态,从而减少内存的开销。

享元模式的组成部分

Flyweight(享元接口):定义对象的接口,通过这个接口可以接受外部状态。

ConcreteFlyweight(具体享元类):实现享元接口,并且存储内部状态。

UnsharedConcreteFlyweight(非共享享元类):不被共享的享元对象,一般不会出现在享元工厂中。

FlyweightFactory(享元工厂类):用来创建和管理享元对象,确保合理地共享享元。

享元模式的实现

在 Java 中实现享元模式,可以通过将对象的内部状态和外部状态分离,并使用享元工厂来管理共享的享元对象。下面是一个详细的示例,展示如何在 Java 中实现享元模式。

享元模式示例

我们将创建一个模拟围棋棋子的应用,其中棋子的颜色是内部状态,而棋子的坐标是外部状态。

复制代码
1. 定义享元接口
// 享元接口
public interface ChessPiece {
    void place(int x, int y);
}
2. 实现具体享元类
// 具体享元类
public class ConcreteChessPiece implements ChessPiece {
    private final String color;  // 内部状态

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

    @Override
    public void place(int x, int y) {
        System.out.println("Placing a " + color + " piece at (" + x + ", " + y + ")");
    }
}
3. 创建享元工厂类
import java.util.HashMap;
import java.util.Map;

// 享元工厂类
public class ChessPieceFactory {
    private static final Map<String, ChessPiece> pieces = new HashMap<>();

    public static ChessPiece getChessPiece(String color) {
        ChessPiece piece = pieces.get(color);
        if (piece == null) {
            piece = new ConcreteChessPiece(color);
            pieces.put(color, piece);
        }
        return piece;
    }
}
4. 客户端代码
public class FlyweightPatternDemo {
    public static void main(String[] args) {
        ChessPiece blackPiece1 = ChessPieceFactory.getChessPiece("Black");
        blackPiece1.place(1, 1);

        ChessPiece blackPiece2 = ChessPieceFactory.getChessPiece("Black");
        blackPiece2.place(2, 2);

        ChessPiece whitePiece1 = ChessPieceFactory.getChessPiece("White");
        whitePiece1.place(3, 3);

        ChessPiece whitePiece2 = ChessPieceFactory.getChessPiece("White");
        whitePiece2.place(4, 4);

        System.out.println("blackPiece1 and blackPiece2 are the same instance: " + (blackPiece1 == blackPiece2));
        System.out.println("whitePiece1 and whitePiece2 are the same instance: " + (whitePiece1 == whitePiece2));
    }
}
运行结果
Placing a Black piece at (1, 1)
Placing a Black piece at (2, 2)
Placing a White piece at (3, 3)
Placing a White piece at (4, 4)
blackPiece1 and blackPiece2 are the same instance: true
whitePiece1 and whitePiece2 are the same instance: true

享元模式总结

在这个示例中,我们通过享元模式有效地减少了棋子对象的创建次数。享元工厂负责创建和管理享元对象,并确保每种颜色的棋子只有一个实例,从而节省内存。棋子的颜色作为内部状态被共享,而棋子的坐标作为外部状态由客户端提供。

享元模式的优缺点

优点:

减少对象的创建,降低内存消耗,提高系统性能。

提高了系统的可扩展性。

缺点:

使系统更加复杂,需要额外的代码来管理内部状态和外部状态的分离。

不适合内外状态较为复杂且不同的对象。

适用场景

享元模式适用于以下场景:

系统中存在大量相似对象,导致内存开销大。

对象的大部分状态可以外部化。

需要缓冲池的场景。

对象的状态可以分为内部状态和外部状态,并且内部状态可以共享。

通过使用享元模式,可以显著减少对象的数量,提高系统性能,特别是在需要大量细粒度对象的应用场景中。

相关推荐
小温冲冲18 小时前
通俗且全面精讲工厂设计模式
设计模式
进击的小头18 小时前
设计模式与C语言高级特性的结合
c语言·设计模式
小温冲冲18 小时前
通俗且全面精讲单例设计模式
开发语言·javascript·设计模式
Vivienne_ChenW19 小时前
DDD领域模型在项目中的实战
java·开发语言·后端·设计模式
sg_knight19 小时前
原型模式(Prototype)
python·设计模式·开发·原型模式
短剑重铸之日20 小时前
《设计模式》第九篇:三大类型之结构型模式
java·后端·设计模式·组合模式·代理模式·结构性模式
忧郁的Mr.Li21 小时前
设计模式--单例模式
javascript·单例模式·设计模式
范纹杉想快点毕业21 小时前
状态机设计模式与嵌入式系统开发完整指南
java·开发语言·网络·数据库·mongodb·设计模式·架构
短剑重铸之日21 小时前
《设计模式》第十篇:三大类型之行为型模式
java·后端·设计模式·责任链模式·访问者模式·行为型模式
YigAin1 天前
Unity23种设计模式之 解释器模式
设计模式·解释器模式