據我瞭解,工廠方法是簡單工廠和工廠對象是抽象工廠? 人和:工廠方法VS工廠對象
- 工廠方法(簡單工廠):
public class SimplePizzaFactory {
public static final int CHEESE = 1;
public static final int PEPPERONI = 2;
public static final int VEGGIE = 3;
public static Pizza createPizza(int type) {
Pizza pizza = null;
if (type == CHEESE) {
pizza = new CheesePizza();
} else if (type == PEPPERONI) {
pizza = new PepperoniPizza();
} else if (type == VEGGIE) {
pizza = new VeggiePizza();
}
return pizza;
}
}
工廠對象(抽象工廠):
?
我對不對?
工廠模式有多少實現,它們有什麼不同?
你是對的,謝謝 – drifter 2012-02-02 19:06:48
另一件事,除非你使用一些古老的Java版本,請使用枚舉而不是整數來枚舉可能的比薩類型。 – Renato 2012-02-02 19:14:07
+1,我發現這個古老的例子(Java的那個時候還沒有枚舉呢。) – drifter 2012-02-02 19:16:56