我在java 库中寻找一个工厂实现,它在决定重新运行哪个对象(而不是swichcase )的部分使用hashMap ,类似于以下想法 -
public class RobotFactory {
private interface RobotFac{
public Robot create();
}
private class RV1Fac implements RobotFac{
public Robot create(){ return new RV1();}
}
private class RV2Fac implements RobotFac{
public Robot create(){ return new RV2();}
}
private class AiboFac implements RobotFac{
public Robot create(){ return new Aibo();}
}
private hashMap<String,RobotFac> robotFactory ;
public RobotFactory(){
robotFactory=new HashMap<String,RobotFac> ();
robotFactory.put("RV1", new RV1Fac());
robotFactory.put("RV2", new RV2Fac());
robotFactory.put("Aibo", new AiboFac());
}
public Robot createRobot(String type){
return robotFactory.get(type).create();
}
}
java库(jre7)中是否存在任何模式?