设计模式——享元模式
介绍
主要用于减少创建对象得数量,以减少内存占用和提高性能。
意图
减少对象的创建,尽量共享同一元素。
解决
当jvm内存中对象太多时,可能会造成内存溢出情况,我们将其中共同的对象抽象出来(即不用一个个的都创建对象),之后有相同的业务请求过来时,返回一个内存中有得对象,避免重新创建。
优点
- 大大减少对象得创建,降低重复内存,提供使用效率。
缺点
- 提高了系统的复杂度。需要分离出内部状态和外部状态,而外部状态具有固化特性,不应该随着内部状态的改变而改变
使用场景
- 内存中的string字符串,如果有则返回该字符串地址,没有则在内存中分配,再返回地址;
- 数据库连接池。
UML
示例
定义一个画图得接口 Shape.java
package cn.geoaryblog.design.cretedg.flyweight;
public interface Shape {
void draw();
}
圆 这个对象,之后要画得就是他, Circle.java
package cn.geoaryblog.design.cretedg.flyweight;
public class Circle implements Shape{
private String color;
private int x;
private int y;
private int radius;
public Circle(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Circle{" +
"color='" + color + '\'' +
", x=" + x +
", y=" + y +
", radius=" + radius +
'}');
}
}
画图工厂,用于动态生成圆对象 ShapeFactory.java
package cn.geoaryblog.design.cretedg.flyweight;
import java.util.HashMap;
public class ShapeFactory {
private static final HashMap<String, Shape> circleMap = new HashMap<>();
// 添加 synchronized 线程锁,防止在多线程情况下生成多个对象
public static synchronized Shape getCircle(String color){
Circle circle = (Circle) circleMap.get(color);
if(circle == null){
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("创建成功"+color);
}
return circle;
}
}
客户端 Client.java
package cn.geoaryblog.design.cretedg.flyweight;
public class Client {
private static final String colors[] = {"red", "green", "blue", "white", "black"};
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);
}
public static void main(String[] args) {
// 10个线程每个线程生成30个对象
for (int i = 0; i < 10; i++) {
new Thread(() -> {
for (int j = 0; j < 30; j++) {
Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.draw();
}
}).start();
}
/*for (int i = 0; i < 30; i++) {
Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.draw();
}*/
}
}
执行结果:
创建成功white
创建成功red
Circle{color='white', x=33, y=98, radius=0}
Circle{color='white', x=29, y=9, radius=0}
创建成功blue
Circle{color='red', x=71, y=18, radius=0}
创建成功black
Circle{color='blue', x=51, y=44, radius=0}
创建成功green
Circle{color='red', x=6, y=35, radius=0}
Circle{color='green', x=87, y=18, radius=0}
Circle{color='green', x=48, y=68, radius=0}
Circle{color='black', x=63, y=63, radius=0}
Circle{color='black', x=89, y=79, radius=0}
Circle{color='red', x=66, y=11, radius=0}
Circle{color='blue', x=71, y=4, radius=0}
Circle{color='blue', x=22, y=61, radius=0}
Circle{color='black', x=69, y=4, radius=0}
... ...
Circle{color='green', x=3, y=10, radius=0}
Circle{color='black', x=1, y=14, radius=0}
Circle{color='black', x=65, y=58, radius=0}
Circle{color='red', x=22, y=31, radius=0}
Circle{color='white', x=65, y=49, radius=0}
Circle{color='green', x=17, y=57, radius=0}
Circle{color='red', x=62, y=49, radius=0}
Circle{color='green', x=83, y=37, radius=0}
Circle{color='black', x=18, y=50, radius=0}
Circle{color='black', x=52, y=76, radius=0}
Circle{color='black', x=44, y=10, radius=0}
Circle{color='white', x=48, y=72, radius=0}
Circle{color='black', x=85, y=47, radius=0}
Circle{color='red', x=90, y=21, radius=0}
Process finished with exit code 0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南