下面是有关抽象工厂和具体工厂的三个裁剪后的类,请前辈们先看下:
public abstract class BasetableFactory{
private static final String classname="com.bwm.database.DbBasetableFactory";
private static BasetableFactory factory;
public static BasetableFacotry getInstance(){
Class c=Class.forName(classname);
factory=(BasetableFactory)c.newInstance();
return new BasetableFactoryProxy(factory);
}
public abstract int getRow(String sql);
}
public class BasetableFactoryProxy extends BasetableFactory{
private BasetableFactory factory;
public BasetableFactoryProxy(BasetableFactory factory){
this.factory=factory;
}
public int getRow(String sql){
return factory.getRow(sql);
}
}
public class DbBasetableFactory extends BasetableFactory{
public DbBasetableFactory(){}
public int getRow(String sql){
return DataBase.getRow(sql);
}
}
我想问的是:案例里这样做好象拐来拐去的封装了好多层,直接使用
抽象工厂BasetableFactory和具体工厂DbBasetableFactory也可以
实现所要的功能,为什么还要再构造多一个类BasetableFactoryProxy来
实例化BasetableFactoryProxy,这样做有什么好处?
谢谢!
------解决方案--------------------
去看看接口和抽象类的作用