Java 这真的是飞锤图案吗?
以下是我正在阅读的教程: 我认为以下代码不是所述的flyweight模式:Java 这真的是飞锤图案吗?,java,design-patterns,Java,Design Patterns,以下是我正在阅读的教程: 我认为以下代码不是所述的flyweight模式: 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.
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("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :" + radius);
}
}
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;
}
}
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);
}
}
公共界面形状{
无效抽取();
}
公共课圈实现形态{
私有字符串颜色;
私人INTX;
私营企业;
私有整数半径;
公众圈(字符串颜色){
这个颜色=颜色;
}
公共无效集合x(整数x){
这个.x=x;
}
公共空间设置(整数y){
这个。y=y;
}
公共空间设置半径(整数半径){
这个半径=半径;
}
@凌驾
公众抽签(){
System.out.println(“圆:Draw()[Color:+Color+”,x:+x+,y:+y+,radius:+radius”);
}
}
导入java.util.HashMap;
公共类形状因子{
private static final HashMap circleMap=new HashMap();
公共静态形状getCircle(字符串颜色){
圆圈=(圆圈)circleMap.get(颜色);
如果(圆==null){
圆圈=新圆圈(颜色);
圆形地图放置(颜色,圆形);
System.out.println(“创建颜色圈:“+color”);
}
返回圈;
}
}
公共类飞行模式演示{
私有静态最终字符串颜色[]={“红色”、“绿色”、“蓝色”、“白色”、“黑色”};
公共静态void main(字符串[]args){
对于(int i=0;i<20;++i){
Circle Circle=(Circle)ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
圆。设定半径(100);
画圆();
}
}
私有静态字符串getRandomColor(){
返回颜色[(int)(Math.random()*colors.length)];
}
私有静态int-getRandomX(){
返回值(int)(Math.random()*100);
}
私有静态int getRandomY(){
返回值(int)(Math.random()*100);
}
}
这似乎不是一个flyweight模式,因为根据wikipedia的说法,“flyweight是一个通过与其他类似对象共享尽可能多的数据来最小化内存使用的对象”。换句话说,我看不到具有内在和外在数据的对象。在这里,我只能看到一个具有某种缓存系统的工厂
有人能演示一下为什么这是或不是飞锤图案吗?当您通过
ShapeFactory
创建一个新的圆时,如果存在一个符合所需颜色的实例,将返回一个已创建的实例。这样,您可以重用创建的circle
(它们“共享”)他们使用相同颜色的圆圈显示数据),并最大限度地减少内存消耗
也就是说,此代码显示了一些问题。例如,圆
对象是可变的,因此,如果您开始修改已创建的圆,则相同颜色的所有其他圆也将被修改
而且它是完全不安全的。请在您的问题中发布相关代码,而不是链接到它添加相关代码。我认为如果颜色是另一个类,工厂缓存颜色而不是形状,那么它将是flyweight。@user1883212 Geeze,这就是我认为正在发生的事情。但是在上下文中它“有意义”hat形状本身正在缓存,因为圆仅在直接作用域中使用(当它绘制自身时)。因此,没有圆实际被“共享”到另一个上下文,因为只有一个实例在使用。(如果由于可变状态而共享它们,那将是不好的。)你的困惑是完全可以理解的。这段代码显然是试图演示Flyweight,但它有缺陷(IMHO没有任何意义),而且教程编写得很差。(该网站上的其他教程看起来很相似。)我会找到其他资源,如or。