使用場(chǎng)景:一類產(chǎn)品有多個(gè)具體的產(chǎn)品族
一, 簡(jiǎn)單工廠模式
又稱靜態(tài)工廠方法模式
-
工廠類角色:這是本模式的核心,含有一定的商業(yè)邏輯和判斷邏輯,用來創(chuàng)建產(chǎn)品
public class Factory { public static final int BUS = 1; public static final int BICYCLE = 2; // public Car createCar(int t) { Car c = null; switch (t) { case BUS: c = new Bus(); break; case BICYCLE: c = new Bicycle(); break; default: break; } return c; } }
二,工廠方法模式
public interface MethodFactory {
Car createCar();
}
三,抽象工廠模式
1,抽象工廠
public interface AbsFactory {
//創(chuàng)建發(fā)動(dòng)機(jī)
Engine createEngine();
//創(chuàng)建空調(diào)
Aircondition createAircondition();
}
2,真正的工廠
public class BicycleFactory implements MyAbsMethodFactory {
@Override
public Car createCar() {
return new Bicycle();
}
}