//工厂接口
public void run();
public void stop();
}
//工厂的实现
public class Benz implements Car {
@Override
public void run() {
System.out.println("Benz开始启动了。。。。。");
}
@Override
public void stop() {
System.out.println("Benz停车了。。。。。");
}
}
public class Ford implements Car {
@Override
public void run() {
System.out.println("Ford开始启动了。。。");
}
@Override
public void stop() {
System.out.println("Ford停车了。。。。");
}
}
//工厂
public class Factory {
public static Car getCarInstance(String type) {
Car c = null;
try {
c = (Car) Class.forName("com.tontisa.Testfactory." + type).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return c;
}
}
//调用地点
public class FactoryImpl {
public static void main(String[] args) {
Car c = Factory.getCarInstance("Ford");// ****这里调用具体的实现类
if (c != null) {
c.run();
c.stop();
} else {
System.out.println("造不了这种汽车。。。");
}
}
}