package Flyweight;
import java.util.HashMap;
public class FlyweightFactory {
private HashMap<String, Flyweight> hashMap;
static FlyweightFactory factory = new FlyweightFactory();
public FlyweightFactory() {
this.hashMap = new HashMap<String, Flyweight>();
}
public static FlyweightFactory getFactory() {
return factory;
}
public synchronized Flyweight getFlyweight(String key) {
if (hashMap.containsKey(key)) {
return hashMap.get(key);
} else {
double width = 0,height = 0,lenght = 0;
String[] str = key.split("#");
width = Double.parseDouble(str[0]);
height = Double.parseDouble(str[1]);
lenght = Double.parseDouble(str[2]);
Flyweight ft = new ConcreteFlyweight(width, height, lenght);
hashMap.put(key, ft);
return ft;
}
}
class ConcreteFlyweight implements Flyweight {
private double width;
private double height;
private double lenght;
public ConcreteFlyweight(double width, double height, double lenght) {
this.width = width;
this.height = height;
this.lenght = lenght;
}
@Override
public double getHeight() {
return height;
}
@Override
public double getWidth() {
return width;
}
@Override
public double getLength() {
return lenght;
}
@Override
public void printMess(String mess) {
System.out.println(mess);
System.out.print("宽度:" + width);
System.out.print("高度:" + height);
System.out.print("长度:" + lenght);
}
}
}