【发布时间】:2016-05-04 00:35:32
【问题描述】:
当需要装箱大量特定类型的物体时,我们会使用享元。因为它们共享一个公共数据(内在状态),所以有助于减少内存消耗,并且还拥有自己的状态(外在状态),它不会在所有其他对象之间共享。这是一个示例代码
public abstract class Shape {
private String color;
private int x;
private int y;
public Shape(String color) {
super();
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 abstract String draw();
@Override
public String toString() {
return "[color=" + color + ", x=" + x + ", y=" + y + "]";
}
}
public class Circle extends Shape {
public Circle(String color) {
super(color);
}
@Override
public String draw() {
// TODO Auto-generated method stub
return "Circle " + toString();
}
}
import java.util.HashMap;
public class ShapeFactory {
private static HashMap<String, Shape> circle_map = new HashMap<>();
public static Shape getCircle(String size){
if(!circle_map.containsKey(size))
circle_map.put(size, new Circle(size));
return circle_map.get(size);
}
}
和司机
public class ShapeDriver {
public static void main(String[] args) {
Shape first_circle = ShapeFactory.getCircle("small");
first_circle.setX(45);
first_circle.setY(12);
Shape second_circle = ShapeFactory.getCircle("small");
System.out.println("First circle" + first_circle);
System.out.println("Second circle" + second_circle);
second_circle.setX(62);
second_circle.setY(23);
System.out.println("First circle" + first_circle);
System.out.println("Second circle" + second_circle);
}
}
在这个例子中,我希望大小是内在的,坐标(x,y)是外在的,但是当我改变 first_circle 的坐标时,它也反映了 second_circle 的变化,因为它们共享一个完整的对象而不是只是一个状态。输出如下
First circle[color=small, x=45, y=12]
Second circle[color=small, x=45, y=12]
First circle[color=small, x=62, y=23]
Second circle[color=small, x=62, y=23]
相当于创建一个对象并将其存储在不同的对象中,但它们都具有相同的状态或数据,那么它们如何拥有自己的数据(外部状态)?
【问题讨论】:
-
请出示您的 ShapeFactory 的代码。
-
你确定享元适合这个吗?您想创建许多具有不同状态(即坐标)的“圆形”对象。 Flyweight 将只能应用于共享状态(即颜色) - 您仍然必须拥有单独的“形状”实例才能将 flyweight 对象与可变状态连接。
-
谢谢@sisyphus 我明白你的意思,我错了蝇量级。颜色是共享状态,那么享元应该只存储
color,并且每次circle的新对象都将使用相同的颜色对象创建,正如@Glenner003 所写。
标签: oop design-patterns flyweight-pattern