享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。
一个形状接口
/**
* 形状接口
*
* @author 吴尚慧
* @since 2022/6/26 13:11
*/
public interface Shape {
/**
* 画
*/
void draw();
}
形状接口实现类:圆
/**
* 圆
*
* @author 吴尚慧
* @since 2022/6/13 10:22
*/
public class Circle implements Shape {
/**
* 颜色
*/
private String color;
/**
* x坐标
*/
private int x;
/**
* y坐标
*/
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("Circle: Draw() [Color : " + color
+ ", x : " + x + ", y :" + y + ", radius :" + radius);
}
}
一个创建形状的工厂
/**
* @author 吴尚慧
* @since 2022/6/26 17:14
*/
public class ShapeFactory {
private static final Map<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;
}
}
测试类
/**
* 测试类
*
* @author 吴尚慧
* @since 2022/6/26 17:15
*/
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);
}
}
结果:
Creating circle of color : Red
Circle: Draw() [Color : Red, x : 33, y :42, radius :100
Creating circle of color : White
Circle: Draw() [Color : White, x : 63, y :80, radius :100
Circle: Draw() [Color : Red, x : 22, y :89, radius :100
Creating circle of color : Black
Circle: Draw() [Color : Black, x : 36, y :28, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 4, y :7, radius :100
Circle: Draw() [Color : White, x : 31, y :63, radius :100
Circle: Draw() [Color : Red, x : 26, y :9, radius :100
Circle: Draw() [Color : Blue, x : 90, y :93, radius :100
Circle: Draw() [Color : Black, x : 82, y :13, radius :100
Circle: Draw() [Color : White, x : 35, y :44, radius :100
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 8, y :78, radius :100
Circle: Draw() [Color : Green, x : 94, y :68, radius :100
Circle: Draw() [Color : Green, x : 98, y :28, radius :100
Circle: Draw() [Color : Black, x : 61, y :59, radius :100
Circle: Draw() [Color : White, x : 44, y :42, radius :100
Circle: Draw() [Color : Black, x : 32, y :74, radius :100
Circle: Draw() [Color : Blue, x : 50, y :18, radius :100
Circle: Draw() [Color : Red, x : 96, y :8, radius :100
Circle: Draw() [Color : Blue, x : 68, y :40, radius :100
Circle: Draw() [Color : Red, x : 8, y :49, radius :100
从结果中可以看出,同一种颜色的圆只会创建一次,执行了20次,最多也只有5个对象,这里随机测试结果是5个对象。
参考:
https://www.runoob.com/design-pattern/design-pattern-intro.html