享元模式
内容参考 w3cschool
分类:结构性设计模式
应用:缓冲池
这也是类似于二手车改装更换零件之后,继续使用,直至n手车。非常节约成本
目录
UML类图
创建形状接口
Shape
public interface Shape {
/**
* 绘制图形
*/
void draw();
}
创建形状实体类
Circle
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.printf("[Circle<%s>] Draw Color: %10s position:(%3d,%3d) radius:%d\n",this,color,x,y,radius);
}
}
Square
public class Square implements Shape{
private String color;
private int width;
private int height;
private int x;
private int y;
public Square(String color) {
this.color = color;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public void draw() {
System.out.printf("[Square<%s>] Draw Color: %10s position:(%3d,%3d) width*height:%3d*%3d\n",this,color,x,y,width,height);
}
}
创建图形工厂
ShapeFactory
public class ShapeFactory {
private static final HashMap<String,Shape> circleMap = new HashMap<>();
private static final HashMap<String,Shape> squareMap = 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.printf("Creating %10s Circle\n",color);
}
return circle;
}
public static Shape getSquare(String color){
Square square = (Square) squareMap.get(color);
if (square == null){
square = new Square(color);
squareMap.put(color,square);
System.out.printf("Creating %10s Square\n",color);
}
return square;
}
}
测试运行
public class ExecuteMain {
public static final String[] colors = {"Red","Blue","Green"};
public static void main(String[] args) {
for (int i = 0;i<10;i++){
Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomNum(100));
circle.setY(getRandomNum(100));
circle.setRadius(getRandomNum(10)+5);
circle.draw();
}
System.out.println();
for (int i = 0;i<10;i++){
Square square = (Square) ShapeFactory.getSquare(getRandomColor());
square.setWidth(getRandomNum(100)+20);
square.setHeight(getRandomNum(60)+10);
square.setX(getRandomNum(100));
square.setY(getRandomNum(100));
square.draw();
}
}
public static String getRandomColor(){
return colors[(int) (Math.random()*colors.length)];
}
public static int getRandomNum(int num){
return (int) (Math.random()*num);
}
}
Creating Green Circle
[Circle<cn.dcpnet.flyweight.Circle@7ea987ac>] Draw Color: Green position:( 75, 67) radius:7
Creating Blue Circle
[Circle<cn.dcpnet.flyweight.Circle@12a3a380>] Draw Color: Blue position:( 14, 8) radius:11
Creating Red Circle
[Circle<cn.dcpnet.flyweight.Circle@29453f44>] Draw Color: Red position:( 52, 0) radius:5
[Circle<cn.dcpnet.flyweight.Circle@29453f44>] Draw Color: Red position:( 54, 25) radius:8
[Circle<cn.dcpnet.flyweight.Circle@7ea987ac>] Draw Color: Green position:( 64, 70) radius:8
[Circle<cn.dcpnet.flyweight.Circle@12a3a380>] Draw Color: Blue position:( 96, 25) radius:6
[Circle<cn.dcpnet.flyweight.Circle@12a3a380>] Draw Color: Blue position:( 27, 14) radius:8
[Circle<cn.dcpnet.flyweight.Circle@7ea987ac>] Draw Color: Green position:( 24, 97) radius:7
[Circle<cn.dcpnet.flyweight.Circle@29453f44>] Draw Color: Red position:( 4, 41) radius:12
[Circle<cn.dcpnet.flyweight.Circle@7ea987ac>] Draw Color: Green position:( 16, 9) radius:14
Creating Green Square
[Square<cn.dcpnet.flyweight.Square@5cad8086>] Draw Color: Green position:( 6, 86) width*height: 86* 36
Creating Red Square
[Square<cn.dcpnet.flyweight.Square@6e0be858>] Draw Color: Red position:( 46, 53) width*height: 51* 58
[Square<cn.dcpnet.flyweight.Square@6e0be858>] Draw Color: Red position:( 19, 85) width*height:110* 52
Creating Blue Square
[Square<cn.dcpnet.flyweight.Square@61bbe9ba>] Draw Color: Blue position:( 28, 1) width*height: 75* 54
[Square<cn.dcpnet.flyweight.Square@6e0be858>] Draw Color: Red position:( 65, 79) width*height: 88* 13
[Square<cn.dcpnet.flyweight.Square@61bbe9ba>] Draw Color: Blue position:( 77, 68) width*height: 93* 52
[Square<cn.dcpnet.flyweight.Square@5cad8086>] Draw Color: Green position:( 84, 32) width*height: 59* 42
[Square<cn.dcpnet.flyweight.Square@6e0be858>] Draw Color: Red position:( 73, 67) width*height: 97* 36
[Square<cn.dcpnet.flyweight.Square@5cad8086>] Draw Color: Green position:( 69, 59) width*height:106* 49
[Square<cn.dcpnet.flyweight.Square@5cad8086>] Draw Color: Green position:( 98, 82) width*height:102* 15
Process finished with exit code 0
可以看出,同一个颜色的 圆形/矩形 对象一直再被重用,只是属性被改变了,这样节省了大量的空间。