java常用设计模式之享元模式
------------------------------ 享元模式(Flyweight) -------------------------------
(1)
主要用于创建对象时,运用共享技术,减少对象对内存的占用.一个提高程序效率和性能的模式,会大大加快程序的运行速度.
就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。Flyweight(享元)模式中常出现Factory模式。Flyweight的内部状态是用来共享的,Flyweight factory负责维护一个对象存储池(Flyweight Pool)来存放内部状态的对象。Flyweight的关键思路,在于:
新建对象时:
先到hashtable中进行获取-->判断取得对象是否为空-->若是,则新建此对象,且放回hashtable -->若存在,则共享原来的对象.(2)
实例: (与静态工厂模式进行对比)
public interface Car {
?public void showCarName();}class BMWCar implements Car
{
?public void showCarName()
?{
??System.out.println("this is the BMWCar .");
?}
}class FordCar implements Car
{
?public void showCarName()
?{
??System.out.println("this is the FordCar .");
?}
}class CarFactory
{
?public static Car car;
?public static Car getCar(String name)
?{
??if("BMW".equals(name))
??{
???car = new BMWCar();
??}
??if("Ford".equals(name))
??{
???car =? new FordCar();
??}
??return car;
?}
}class CarFlyWeightFactory
{
??? public? Car car;
??? private Hashtable<String,Car> carPool=new Hashtable<String,Car>();
?public? Car getCar(String name)
?{
??if("BMW".equals(name))
??{
???car=carPool.get(name);
???if(car==null)
???{
????car=new BMWCar();
????carPool.put(name, car);
???}
??}
??
??if("Ford".equals(name))
??{
???car=carPool.get(name);
???if(car==null)
???{
????car=new FordCar();
????carPool.put(name, car);
???}
??}
??return car;
?}
??????? public int getNumber(){ return carPool.getSize(); }
}
public class Test {?public static void main(String[] args) {
??CarFlyWeightFactory carFlyWeightFactory=new CarFlyWeightFactory();
??Car carf1=carFlyWeightFactory.getCar("Ford");
??carf1.showCarName();
??Car carf2=carFlyWeightFactory.getCar("Ford");
??carf2.showCarName();
??if(carf1==carf2)
??{
???System.out.println("同一部车来的");
??}
??else
??{
???System.out.println("不同一部车来的");
??}
??System.out.println("车的数量是:"+carFlyWeightFactory.getNumber());
?}
}输出:
this is the FordCar .
this is the FordCar .
同一部车来的转自:http://shenzhenchufa.blog.51cto.com/730213/161581