package xx.demo2;
/**
* 工厂模式:根据传入的参数不同,由工厂来创建哪一种类的实例
* IOC采用了工厂模式,由工厂来创建相应的对象,采用反射+xml实现
* @author xx
*
*/
public class FactoryPattern {
public static void main(String[] args) {
Car aoDi = CarFactory.createCar("奥迪");
Car benChi = CarFactory.createCar("奔驰");
aoDi.run();
benChi.run();
}
}
interface Car{
void run();
}
class AoDi implements Car{
public void run() {
System.out.println("奥迪..................");
}
}
class BenChi implements Car{
public void run() {
System.out.println("奔驰.................");
}
}
class CarFactory{
public static Car createCar(String name) {
Car car = null;
switch (name) {
case "奥迪":
car = new AoDi();
break;
case "奔驰":
car = new BenChi();
break;
default:
break;
}
return car;
}
}