通过共享已经存在的对象来减少内存消耗,从而提高系统性能。享元模式特别适用于那些数量庞大、创建代价高昂、且内容相同或相似的对象。
示例
假设我们有一个绘图应用程序,需要绘制大量的圆形对象。每个圆形对象具有颜色、位置和大小属性。使用享元模式,我们可以将颜色作为内部状态共享,而位置和大小作为外部状态。
步骤1:定义享元接口和具体享元类
// 享元接口
public interface Shape {
void draw();
}
// 具体享元类
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 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("Drawing Circle [Color: " + color + ", x: " + x + ", y: " + y + ", radius: " + radius + "]");
}
}
步骤2:创建享元工厂
import java.util.HashMap;
public class ShapeFactory {
private static final HashMap<String, Shape> circleMap = new HashMap<>();
public static Shape getCircle(String color) {
Circle circle = (Circle) circleMap.get(color);
if (circle == null) {
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("Creating circle of color : " + color);
}
return circle;
}
}
步骤3:测试享元模式
public class FlyweightPatternDemo {
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() {
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);
}
}
解释示例中的代码
- 享元接口 Shape:定义了绘制图形的方法 draw。
- 具体享元类 Circle:实现了 Shape 接口,并包含了内部状态 color 和外部状态 x, y, radius。内部状态
color 在创建时确定,而外部状态通过 setter 方法设置。 - 享元工厂 ShapeFactory:管理享元对象的创建和共享。它通过颜色作为键来存储和返回共享的 Circle 对象。如果一个颜色的 Circle 对象不存在,它将创建一个新的对象并放入缓存。
- 测试类 FlyweightPatternDemo:测试享元模式的应用。它通过 ShapeFactory 获取共享的 Circle
对象,并设置外部状态来绘制不同的圆形。