[color=red]【享元模式】运用共享技术有效地支持大量细粒度的对象[/color]
package com.demo.flyweight;
//享元接口
public interface Flyweight {
public double getHeight();
public double getWidth();
public double getLength();
//使用参数mess获取外部数据
public void printMess(String Mess);
}
package com.demo.flyweight;
import java.util.HashMap;
//享元工厂
public class FlyweightFactory {
private HashMap<String, Flyweight> hashMap;
static FlyweightFactory factory = new FlyweightFactory();
private FlyweightFactory() {
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, length = 0;
String[] str = key.split("#");
width = Double.parseDouble(str[0]);
height = Double.parseDouble(str[1]);
length = Double.parseDouble(str[2]);
Flyweight ft = new ConcreteFlyweight(width, height, length);
hashMap.put(key, ft);
return ft;
}
}
//ConcreteFlyweight是内部类
class ConcreteFlyweight implements Flyweight {
private double width;
private double height;
private double length;
private ConcreteFlyweight(double width, double height, double length) {
this.width = width;
this.height = height;
this.length = length;
}
@Override
public double getHeight() {
// TODO Auto-generated method stub
return height;
}
@Override
public double getWidth() {
// TODO Auto-generated method stub
return width;
}
@Override
public double getLength() {
// TODO Auto-generated method stub
return length;
}
@Override
public void printMess(String Mess) {
// TODO Auto-generated method stub
//输出外部数据mess
System.out.print(Mess);
//输出内部数据width
System.out.print("宽度:" + width);
System.out.print("高度:" + height);
System.out.println("长度:" + length);
}
}
}
package com.demo.flyweight;
public class Car {
//存放享元对象的引用
Flyweight flyweight;
String name,color;
int power;
Car(Flyweight flyweight,String name,String color,int power){
this.flyweight=flyweight;
this.name=name;
this.color=color;
this.power=power;
}
public void print(){
System.out.print("名称:"+name);
System.out.print("颜色:"+color);
System.out.print("功率:"+power);
System.out.print("宽度:"+flyweight.getWidth());
System.out.print("高度:"+flyweight.getHeight());
System.out.println("长度:"+flyweight.getLength());
}
}
package com.demo.flyweight;
public class Application {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FlyweightFactory factory=FlyweightFactory.getFactory();
double width=1.8,height=1.47,length=5.12;
String key=""+width+"#"+height+"#"+length;
Flyweight flyweight=factory.getFlyweight(key);
Car audiA6One=new Car(flyweight,"奥迪A6","黑色",128);
Car audiA6Two=new Car(flyweight,"奥迪A6","灰色",160);
audiA6One.print();
audiA6Two.print();
width=1.77;
height=1.45;
length=4.63;
key=""+width+"#"+height+"#"+length;
flyweight=factory.getFlyweight(key);
Car audiA4One=new Car(flyweight,"奥迪A4","黑色",126);
Car audiA4Two=new Car(flyweight,"奥迪A4","灰色",138);
flyweight.printMess("名称:奥迪 A4 颜色:蓝色 功率:126");
flyweight.printMess("名称:奥迪 A6 颜色:红色 功率:138");
}
}
名称:奥迪A6颜色:黑色功率:128宽度:1.8高度:1.47长度:5.12
名称:奥迪A6颜色:灰色功率:160宽度:1.8高度:1.47长度:5.12
名称:奥迪 A4 颜色:蓝色 功率:126宽度:1.77高度:1.45长度:4.63
名称:奥迪 A6 颜色:红色 功率:138宽度:1.77高度:1.45长度:4.63