package D_Factory.a;
/**
* 只适用一个地区的披萨店,当在别地建立披萨店时,由于地区口味差异,一个工厂无法满足。 解决方案,根据地区建立多个工厂
*
*/
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();
}
return pizza;
}
}
package D_Factory.a;
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;
}
}
package D_Factory.a;
import D_Factory.d.Dough;
public abstract class Pizza {
public String name;
public Dough dough;
public String sauce;
public String cheese;
public String clam;
public void prepare() {
System.out.println("prepare");
}
public void bake() {
System.out.println("bake");
}
public void cut() {
System.out.println("cut");
}
public void box() {
System.out.println("box");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package D_Factory.a;
public class PepperoniPizza extends Pizza {
}
package D_Factory.a;
public class CheesePizza extends Pizza {
}