一、
1."工厂模式"不是种真正的设计模式,而是一种编程术语
2.The Factory Method Pattern defi nes an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
3.The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
二、简单工厂
1.根据情况,new 出来的对象是不同类型的子类,所以会变化是初始化部分,所以根据OO原则,把初始化操作抽取出来封装
2.把产生的不同类型对象的if elseif else new xxx(),抽离出来有什么好处?答:(1)可以为不同的客户端提供相同的初始化机制(2)
3.
4.
package headfirst.designpatterns.factory.pizzas; import java.util.ArrayList; abstract public class Pizza {
String name;
String dough;
String sauce;
ArrayList<String> toppings = new ArrayList<String>(); public String getName() {
return name;
} public void prepare() {
System.out.println("Preparing " + name);
} public void bake() {
System.out.println("Baking " + name);
} public void cut() {
System.out.println("Cutting " + name);
} public void box() {
System.out.println("Boxing " + name);
} public String toString() {
// code to display pizza name and ingredients
StringBuffer display = new StringBuffer();
display.append("---- " + name + " ----\n");
display.append(dough + "\n");
display.append(sauce + "\n");
for (String topping : toppings) {
display.append(topping + "\n");
}
return display.toString();
}
}
5.
package headfirst.designpatterns.factory.pizzas; public class CheesePizza extends Pizza {
public CheesePizza() {
name = "Cheese Pizza";
dough = "Regular Crust";
sauce = "Marinara Pizza Sauce";
toppings.add("Fresh Mozzarella");
toppings.add("Parmesan");
}
}
6.
package headfirst.designpatterns.factory.pizzas; public class PizzaStore {
SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) {
this.factory = factory;
} public Pizza orderPizza(String type) {
Pizza pizza; pizza = factory.createPizza(type); pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box(); return pizza;
} }
7.
package headfirst.designpatterns.factory.pizzas; public class SimplePizzaFactory { public Pizza createPizza(String type) {
Pizza pizza = null; if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("pepperoni")) {
pizza = new PepperoniPizza();
} else if (type.equals("clam")) {
pizza = new ClamPizza();
} else if (type.equals("veggie")) {
pizza = new VeggiePizza();
}
return pizza;
}
}
三、工厂方法模式
1.上面简单工厂的创建pizza是绑定在createPizza()里的,不灵活,可以把createPizza()改为抽象方法,由不同如ChicagoPizzaStore、NYPizzaStore去实现,则可以解耦,实现运行时绑定
The following guidelines can help you avoid OO designs that violate the Dependency Inversion Principle:
No variable should hold a reference to a concrete class.
No class should derive from a concrete class.
No method should override an implemented method of any of its base classes.--》If you override an implemented method,then your base class wasn’t really an abstraction to start with. Those methods implemented in the base class are
meant to be shared by all your subclasses.
2.
3.
4.
5.抽象工厂其实就是在两个维度上抽象(1)在产品上(2)在creator上,注意在creator中有抽象了一个factoryMehtod(),如下图
6.
package headfirst.designpatterns.factory.pizzafm; public abstract class PizzaStore { abstract Pizza createPizza(String item); public Pizza orderPizza(String type) {
Pizza pizza = createPizza(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
7.
package headfirst.designpatterns.factory.pizzafm; public class ChicagoPizzaStore extends PizzaStore { Pizza createPizza(String item) {
if (item.equals("cheese")) {
return new ChicagoStyleCheesePizza();
} else if (item.equals("veggie")) {
return new ChicagoStyleVeggiePizza();
} else if (item.equals("clam")) {
return new ChicagoStyleClamPizza();
} else if (item.equals("pepperoni")) {
return new ChicagoStylePepperoniPizza();
} else return null;
}
}
8.
package headfirst.designpatterns.factory.pizzafm; public class NYPizzaStore extends PizzaStore { Pizza createPizza(String item) {
if (item.equals("cheese")) {
return new NYStyleCheesePizza();
} else if (item.equals("veggie")) {
return new NYStyleVeggiePizza();
} else if (item.equals("clam")) {
return new NYStyleClamPizza();
} else if (item.equals("pepperoni")) {
return new NYStylePepperoniPizza();
} else return null;
}
}
9.
package headfirst.designpatterns.factory.pizzafm; public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza() {
name = "Chicago Style Deep Dish Cheese Pizza";
dough = "Extra Thick Crust Dough";
sauce = "Plum Tomato Sauce"; toppings.add("Shredded Mozzarella Cheese");
} void cut() {
System.out.println("Cutting the pizza into square slices");
}
}
10.
package headfirst.designpatterns.factory.pizzafm; public class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() {
name = "NY Style Sauce and Cheese Pizza";
dough = "Thin Crust Dough";
sauce = "Marinara Sauce"; toppings.add("Grated Reggiano Cheese");
}
}
四、抽象工厂
1.
2.
3.
package headfirst.designpatterns.factory.pizzaaf; public abstract class PizzaStore { protected abstract Pizza createPizza(String item); public Pizza orderPizza(String type) {
Pizza pizza = createPizza(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
4.
package headfirst.designpatterns.factory.pizzaaf; public class ChicagoPizzaIngredientFactory
implements PizzaIngredientFactory
{ public Dough createDough() {
return new ThickCrustDough();
} public Sauce createSauce() {
return new PlumTomatoSauce();
} public Cheese createCheese() {
return new MozzarellaCheese();
} public Veggies[] createVeggies() {
Veggies veggies[] = { new BlackOlives(),
new Spinach(),
new Eggplant() };
return veggies;
} public Pepperoni createPepperoni() {
return new SlicedPepperoni();
} public Clams createClam() {
return new FrozenClams();
}
}
5.
package headfirst.designpatterns.factory.pizzaaf; public abstract class Pizza {
String name; Dough dough;
Sauce sauce;
Veggies veggies[];
Cheese cheese;
Pepperoni pepperoni;
Clams clam; abstract void prepare(); void bake() {
System.out.println("Bake for 25 minutes at 350");
} void cut() {
System.out.println("Cutting the pizza into diagonal slices");
} void box() {
System.out.println("Place pizza in official PizzaStore box");
} void setName(String name) {
this.name = name;
} String getName() {
return name;
} public String toString() {
StringBuffer result = new StringBuffer();
result.append("---- " + name + " ----\n");
if (dough != null) {
result.append(dough);
result.append("\n");
}
if (sauce != null) {
result.append(sauce);
result.append("\n");
}
if (cheese != null) {
result.append(cheese);
result.append("\n");
}
if (veggies != null) {
for (int i = 0; i < veggies.length; i++) {
result.append(veggies[i]);
if (i < veggies.length-1) {
result.append(", ");
}
}
result.append("\n");
}
if (clam != null) {
result.append(clam);
result.append("\n");
}
if (pepperoni != null) {
result.append(pepperoni);
result.append("\n");
}
return result.toString();
}
}
6.
package headfirst.designpatterns.factory.pizzaaf; public interface Dough {
public String toString();
}
7.
package headfirst.designpatterns.factory.pizzaaf; public class ThickCrustDough implements Dough {
public String toString() {
return "ThickCrust style extra thick crust dough";
}
}
8.
package headfirst.designpatterns.factory.pizzaaf; public class PizzaTestDrive { public static void main(String[] args) {
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore(); Pizza pizza = nyStore.orderPizza("cheese");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("cheese");
System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("clam");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("clam");
System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("pepperoni");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("pepperoni");
System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("veggie");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("veggie");
System.out.println("Joel ordered a " + pizza + "\n");
}
}
9.
五、工厂方法与抽象工厂的对比
1.
2.
3.