* 抽象产品类
* @author c_qiaoding
*
*/
public interface Car {
void gotoWork();
}
/**
* 具体产品类
* @author c_qiaoding
*
*/
public class Bike implements Car {
@Override
public void gotoWork() {
System.out.println("骑自行车去上班!");
}
}
/**
* 具体产品类
* @author c_qiaoding
*
*/
public class Bus implements Car {
@Override
public void gotoWork() {
System.out.println("坐公交车去上班!");
}
}
/**
* 抽象工厂类
* @author c_qiaoding
*
*/
public interface ICarFactory {
Car getCar();
}
/**
* 具体产品工厂类
* @author c_qiaoding
*
*/
public class BikeFactory implements ICarFactory {
@Override
public Car getCar() {
return new Bike();
}
}
/**
* 具体产品工厂类
* @author c_qiaoding
*
*/
public class BusFactory implements ICarFactory {
@Override
public Car getCar() {
return new Bus();
}
}
/**
* 测试类
* @author c_qiaoding
*
*/
public class Test {
public static void main(String[] args) {
ICarFactory carFactory = null;
carFactory = new BikeFactory();
carFactory.getCar().gotoWork();
carFactory = new BusFactory();
carFactory.getCar().gotoWork();
}
}
控制台输出:
骑自行车去上班!
坐公交车去上班!