标签:table slice 工厂设计 mil asto soft str shc pat
package com.DesignPatterns.ad.factory3;
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; } |
public class NYPizzaStore extends PizzaStore {
protected Pizza createPizza(String item) {
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory =
new NYPizzaIngredientFactory();
if (item.equals("cheese")) {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("纽约风格的奶酪披萨");
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("纽约风格的素食披萨");
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
pizza.setName("纽约风格的蛤蜊披萨");
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("纽约风格的意大利香肠披萨");
}
return pizza;
}
}
public class ChicagoPizzaStore extends PizzaStore {
protected Pizza createPizza(String item) {
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory =
new ChicagoPizzaIngredientFactory();
if (item.equals("cheese")) {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("芝加哥奶酪披萨");
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("芝加哥风格的素食披萨");
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory); pizza.setName("芝加哥式蛤蜊披萨");
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("芝加哥风格的意大利香肠披萨");
}
return pizza;
}
}
public interface PizzaIngredientFactory {
public Dough createDough();// 创建面团
public Sauce createSauce();// 创建酱汁
public Cheese createCheese();// 创建奶酪
public Veggies[] createVeggies();// 创建蔬菜
public Pepperoni createPepperoni();// 创建意大利香肠 public Clams createClam();// 创建蛤
}
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
public Dough createDough() {
return new ThinCrustDough();
}
public Sauce createSauce() {
return new MarinaraSauce();
}
public Cheese createCheese() {
return new ReggianoCheese();
}
public Veggies[] createVeggies() {
Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() };
return veggies;
}
public Pepperoni createPepperoni() {
return new SlicedPepperoni(); }
public Clams createClam() {
return new FreshClams();
}
}
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(); } |
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("在350度下烤25分钟"); } void cut() { System.out.println("把披萨切成对角线"); } void box() { System.out.println("将披萨放入正式的披萨店"); } void setName(String name) { this.name = name; |
| } String
} public
| getName() { return name; 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(); |
} | } |
public static void main(String[] args) {
PizzaStore nyStore = new NYPizzaStore();
Pizza pizza = nyStore.orderPizza("cheese");
// 1,找到PizzaStore抽象类中来,因为这个时候nyStore是NYPizzaStore
// 2,调用了NYPizzaStore的createPizza方法
// 3,在NYPizzaStore的方法中有NYPizzaIngredientFactory实例,所以这个时候调用了
System.out.println("伊桑下令 " + pizza + "=======");
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;
}
}
public class NYPizzaStore extends PizzaStore {
protected Pizza createPizza(String item) {
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory =
new NYPizzaIngredientFactory();
if (item.equals("cheese")) {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("纽约风格的奶酪披萨");
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("纽约风格的素食披萨");
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
pizza.setName("纽约风格的蛤蜊披萨");
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("纽约风格的意大利香肠披萨");
}
return pizza;
}
}
public interface PizzaIngredientFactory {
public Dough createDough();// 创建面团
public Sauce createSauce();// 创建酱汁
public Cheese createCheese();// 创建奶酪
public Veggies[] createVeggies();// 创建蔬菜
public Pepperoni createPepperoni();// 创建意大利香肠
public Clams createClam();// 创建蛤
}
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
public Dough createDough() {
return new ThinCrustDough();
}
public Sauce createSauce() {
return new MarinaraSauce();
}
public Cheese createCheese() {
return new ReggianoCheese();
}
public Veggies[] createVeggies() {
Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() };
return veggies;
}
public Pepperoni createPepperoni() {
return new SlicedPepperoni(); }
public Clams createClam() {
return new FreshClams();
}
}
public interface Dough { public String toString();
}
public class ThinCrustDough implements Dough {
public String toString() {
return "薄的地壳面团";
}
}
public interface Sauce { public String toString();
}
public class MarinaraSauce implements Sauce {
public String toString() {
return "加番茄酱";
}
}
public interface Cheese { public String toString();
}
public class ReggianoCheese implements Cheese {
public String toString() {
return "雷奇亚干酪奶酪";
}
}
public static void main(String[] args) {
PizzaStore chicagoStore = new ChicagoPizzaStore();
// 1,找到PizzaStore抽象类中来,因为这个时候nyStore是NYPizzaStore
// 2,调用了NYPizzaStore的createPizza方法
// 3,在NYPizzaStore的方法中有NYPizzaIngredientFactory实例,所以这个时候调用了
Pizza pizza = chicagoStore.orderPizza("cheese");
System.out.println("乔尔下令 " + pizza + "=======");
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;
}
}
public class ChicagoPizzaStore extends PizzaStore {
protected Pizza createPizza(String item) {
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory =
new ChicagoPizzaIngredientFactory();
if (item.equals("cheese")) {
pizza = new CheesePizza(ingredientFactory);
pizza.setName("芝加哥奶酪披萨");
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("芝加哥风格的素食披萨");
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory); pizza.setName("芝加哥式蛤蜊披萨");
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("芝加哥风格的意大利香肠披萨");
}
return pizza;
}
}
public interface PizzaIngredientFactory {
public Dough createDough();// 创建面团
public Sauce createSauce();// 创建酱汁
public Cheese createCheese();// 创建奶酪
public Veggies[] createVeggies();// 创建蔬菜
public Pepperoni createPepperoni();// 创建意大利香肠
} | public Clams createClam();// 创建蛤 |
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();
}
}
public interface Dough { public String toString();
}
public class ThickCrustDough implements Dough {
public String toString() {
return "厚皮面团";
}
}
public interface Sauce { public String toString();
}
public class PlumTomatoSauce implements Sauce {
public String toString() {
return "蕃茄酱配梅子蕃茄";
}
}
public interface Cheese { public String toString();
}
public class MozzarellaCheese implements Cheese {
public String toString() {
return "马苏里拉奶酪丝";
}
}
可不可以这样理解抽象工厂是比工厂更抽象的工厂,以前的工厂方法中海油具体的实现,而现在抽象工厂中全部都是抽象的方法。
其实还可以这样理解,工厂定义是让子类来决定自己到底是谁,可能是披萨1,披萨2,披萨3等等。接下来开始做披萨该做的事情。而抽象的则是不需要返回具体了类,比如说现在返回披萨的总和,又返回面包的总和,当我用的时候可能是披萨1+面包1,披萨1+面包2,披萨2+面包1,等等,更倾向于组合。关注点不一样,工厂关注与返回当前的产品的实例,而抽象则关注与返回一组数据的组合。
当产品只有一个的时候抽象工厂变成工厂,当产品有多个的时候工厂变成抽象工厂。
标签:table slice 工厂设计 mil asto soft str shc pat
原文地址:https://www.cnblogs.com/qingruihappy/p/9739648.html