设计模式:享元模式案例

让我们以游戏开发中的棋类游戏(例如国际象棋)为例来展示享元模式的代码实现。在这个例子中,棋子的类型是内部状态,而棋子的位置是外部状态。

Java 代码示例

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

// 享元接口
interface ChessPieceFlyweight {
    void move(int x, int y); // 外部状态:位置
    String getColor(); // 内部状态:颜色
}

// 具体享元类
class ConcreteChessPiece implements ChessPieceFlyweight {
    private final String color; // 内部状态

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

    @Override
    public void move(int x, int y) {
        System.out.printf("Moving %s piece to (%d,%d).\n", color, x, y);
    }

    @Override
    public String getColor() {
        return color;
    }
}

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

    public static ChessPieceFlyweight getChessPiece(String color) {
        if (!pieces.containsKey(color)) {
            ChessPieceFlyweight piece = new ConcreteChessPiece(color);
            pieces.put(color, piece);
        }
        return pieces.get(color);
    }
}

// 客户端代码
public class FlyweightExample {
    public static void main(String[] args) {
        ChessPieceFlyweight whitePiece = ChessPieceFactory.getChessPiece("White");
        ChessPieceFlyweight blackPiece = ChessPieceFactory.getChessPiece("Black");

        // 棋子被多次移动到不同位置,但对象是共享的
        whitePiece.move(1, 2);
        blackPiece.move(2, 3);
        whitePiece.move(4, 5);
        // ... 其他棋子移动操作
    }
}

在这个例子中,ChessPieceFlyweight 接口定义了棋子的外部状态方法 move 和内部状态方法 getColorConcreteChessPiece 类实现了这个接口,并持有内部状态 color,它在棋子的生命周期中是不变的。

ChessPieceFactory 类是享元工厂,它确保同一颜色的棋子只创建一次,并在之后的请求中返回已创建的实例。这样,即使在棋盘上有多个同色棋子,实际上它们都是由同一个享元实例代表的。

客户端代码通过享元工厂获取棋子实例,并多次调用 move 方法来改变棋子的位置,这些位置信息作为外部状态在每次调用时传入。

这个例子展示了享元模式如何在需要大量相似对象的情况下减少内存消耗,通过共享实例来避免重复创建相同或相似的对象。

相关推荐
影寂ldy10 小时前
C# 多接口、同名冲突、显式实现、接口继承 完整笔记
java·笔记·c#
JAVA面经实录91710 小时前
Spring Cloud Alibaba 微服务企业实战完整文档(架构+规范+调优+故障+源码)
java·运维·spring cloud·微服务
布局呆星10 小时前
Spring Boot + JWT + Spring Security 认证授权实战:双角色、双 Token、方法级权限,一次讲透
java·开发语言
大G的笔记本10 小时前
生产级 Spring Boot 网关完整实现方案
java·笔记·gateway
LucianaiB10 小时前
Swarm管理面板的多项目配置策略与模型别名机制的效率分析
java·服务器·前端
qq_25183645710 小时前
基于Spring Boot的数据标注与质检系统设计与实现
java·spring boot·后端
總鑽風10 小时前
Spring AI实战:快速集成阿里通义千问
java·后端·spring·ai编程
一条泥憨鱼10 小时前
苍穹外卖【day3|菜品管理】
java·数据库·sql·mysql·mybatis
Wenzar_11 小时前
Playwright 实战:高可信 UI 回归验证流水线
java·ui
livemetee11 小时前
Java 25虚拟线程使用实例
java