欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 【设计模式】享元模式

【设计模式】享元模式

2025/6/6 0:12:57 来源:https://blog.csdn.net/liberalliushahe/article/details/146980185  浏览:    关键词:【设计模式】享元模式

简介

假设你在开发一款射击游戏,游戏中有成千上万的子弹需要渲染。如果每个子弹都独立存储颜色、大小等固定属性,会占用大量内存。
享元模式的解决方案是:
将子弹的固定属性(如颜色、形状)提取为共享的“模板”(内部状态),每个子弹实例只存储位置、速度等动态属性(外部状态)。
所有子弹共享同一个模板,避免重复创建相同的对象。

适用场景:

系统中存在大量相似对象,且内存开销较大。
对象的大部分状态可以外部化,仅少部分需要独立存储。
需要缓存池或对象复用的场景(如线程池、字符常量池)。

优点:

大幅减少内存占用。
提高性能(通过共享对象)。
缺点:
需要分离内部状态和外部状态,增加系统复杂度。
线程安全问题需额外处理。

类图

在这里插入图片描述

代码

import java.util.HashMap;
import java.util.Map;// 享元接口
interface BulletType {
void shoot(int x, int y);
}// 具体享元类:子弹类型
class ConcreteBulletType implements BulletType {
private final String color; // 内部状态(固定)
private final int size;public ConcreteBulletType(String color, int size) {
this.color = color;
this.size = size;
}@Override
public void shoot(int x, int y) {
System.out.printf("%s子弹(大小%d)发射到位置 (%d, %d)\n", color, size, x, y);
}
}// 享元工厂:管理子弹类型
class BulletFactory {
private static final Map<String, BulletType> bulletTypes = new HashMap<>();public static BulletType getBulletType(String color, int size) {
String key = color + size;
if (!bulletTypes.containsKey(key)) {
bulletTypes.put(key, new ConcreteBulletType(color, size));
System.out.println("创建新子弹类型: " + key);
}
return bulletTypes.get(key);
}
}// 客户端
public class FlyweightDemo {
public static void main(String[] args) {
BulletType redBullet = BulletFactory.getBulletType("红色", 10);
redBullet.shoot(100, 200);BulletType blueBullet = BulletFactory.getBulletType("蓝色", 8);
blueBullet.shoot(50, 150);BulletType redBullet2 = BulletFactory.getBulletType("红色", 10); // 复用已有对象
redBullet2.shoot(300, 400);
}
}

实际场景

Java 的字符串常量池是享元模式的经典应用:相同的字符串字面量共享同一个对象。
public class StringPoolDemo {
public static void main(String[] args) {
String s1 = "Hello"; // 从常量池获取
String s2 = "Hello"; // 复用常量池对象
String s3 = new String("Hello"); // 强制创建新对象System.out.println(s1 == s2); // true(共享对象)
System.out.println(s1 == s3); // false(不同对象)
}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词