package D_Factory.d;
public class CheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
}
}
package D_Factory.d;
public class CheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public CheesePizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
}
}
package D_Factory.d;
public class ClamPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ClamPizza(PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
//需要的材料跟工厂要,不同风味的披萨可以使用原料工厂处理地区差异,而不需要设计不同的类进行处理
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
cheese = ingredientFactory.createClam();
}
}
package D_Factory.d;
public interface Dough {
}
package D_Factory.d;
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
// 可以创建具体的dough材料
public Dough createDough() {
return new ThickCrustDough();
}
public String createSauce() {
return null;
}
public String createCheese() {
return null;
}
public String createClam() {
return null;
}
}
class ThickCrustDough implements Dough {
}
package D_Factory.d;
/*
* 需要的材料跟工厂要,不同风味的披萨可以使用原料工厂处理地区差异,而不需要设计不同的类进行处理
*/
public class NYPizzaStore extends PizzaStore {
@Override
protected Pizza createPizza(String type) {
PizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory();
Pizza pizza = null;
if (type.equals("cheese")) {
pizza = new CheesePizza(pizzaIngredientFactory);
pizza.setName("cheese");
} else if (type.equals("clam")) {
pizza = new ClamPizza(pizzaIngredientFactory);
pizza.setName("clam");
}
return pizza;
}
}
package D_Factory.d;
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.d;
public interface PizzaIngredientFactory {
public Dough createDough();
public String createSauce();
public String createCheese();
public String createClam();
}
package D_Factory.d;
/**
* 由于做披萨做法有差异,使用其他厂商的盒子等等
* 解决让披萨制作局限于pizzaStore类,同时又能让加盟店依然可以自由制作该地区风味的披萨
*/
public abstract class PizzaStore {
public final Pizza orderPizza(String type) {
Pizza pizza;
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// 让子类决定创建何种口味风格的披萨,此方法如同工厂
//工厂方法用来处理对象的创建,并将这样的行为封装在子类中,这样客户程序中关于超类的代码和子类对象创建代码解耦了
protected abstract Pizza createPizza(String type);
}