效果
出海打鱼~~~~面团:普通面包团酱料:普通酱料烤 25 分钟切成6片装箱========================================[海鲜披萨 杭州出品]====原料场地:杭州披萨原料加工厂========================================面团:普通面包团酱料:普通酱料烤 25 分钟切片,铺上芝士装箱========================================[芝士披萨 杭州出品]====原料场地:杭州披萨原料加工厂========================================做不出来,用普通披萨代替出海打鱼~~~~面团:普通面包团酱料:普通酱料烤 25 分钟切成6片装箱========================================[普通披萨 杭州出品]====原料场地:杭州披萨原料加工厂========================================出海打鱼~~~~面团:北京面包团酱料:广西酱料烤 25 分钟切成6片装箱========================================[海鲜披萨 福建出品]====原料场地:福建披萨原料加工厂========================================做不出来,用普通披萨代替出海打鱼~~~~面团:北京面包团酱料:广西酱料烤 25 分钟切成6片装箱========================================[普通披萨 福建出品]====原料场地:福建披萨原料加工厂========================================面团:北京面包团酱料:广西酱料烤 25 分钟切成6片火箭打包----发射!========================================[火箭披萨 福建出品]====原料场地:福建披萨原料加工厂========================================
说明
觉得就是俩个工厂模式的集合,不过用来接口和抽象类
使程序更灵活,修改也更方便。
不过逻辑有点乱,捋清楚了还是很方便的。
完整代码
package factoryMode;// ============ 原料相关 =================class Dough{public String destation() {// TODO Auto-generated method stubreturn "普通面包团";}}class BeijinDough extends Dough{public String destation() {// TODO Auto-generated method stubreturn "北京面包团";}}class Sauce{public String destation() {// TODO Auto-generated method stubreturn "普通酱料";}}class GuanxiSauce extends Sauce{public String destation() {// TODO Auto-generated method stubreturn "广西酱料";}}//============ 原料加工厂相关 =================// 披萨原料工厂接口interface PizzaIngredientFactory{public Dough CreateDough();public Sauce CreateSauce();public String getName();}// 杭州原料加工厂class HanzhouPizzaIngredientFactory implements PizzaIngredientFactory{public Dough CreateDough(){return new Dough();}public Sauce CreateSauce(){return new Sauce();}public String getName() {return "杭州披萨原料加工厂";}}// 福建原料加工厂class FujianPizzaIngredientFactory implements PizzaIngredientFactory{public Dough CreateDough(){return new BeijinDough();}public Sauce CreateSauce(){return new GuanxiSauce();}public String getName() {return "福建披萨原料加工厂";}}//============ 披萨相关 =================// 披萨抽象类abstract class Pizza{public String name;String IngredientFactoryName;Dough dough;Sauce sauce;public Pizza() {// TODO Auto-generated constructor stubname = "普通披萨";}abstract void prepare();void bake() {System.out.println("烤 25 分钟");}void cut() {System.out.println("切成6片");}void box() {System.out.println("装箱");}void lable() {System.out.println("======================================");System.out.println("==[" + name + "]==");System.out.println("==原料场地:" + IngredientFactoryName + "==");System.out.println("======================================");}}//普通披萨class NormolPizza extends Pizza{PizzaIngredientFactory pizzaIngredientFactory;public NormolPizza(PizzaIngredientFactory pizzaIngredientFactory) {name = "普通披萨";this.pizzaIngredientFactory = pizzaIngredientFactory;IngredientFactoryName = pizzaIngredientFactory.getName();}void prepare() {this.dough = pizzaIngredientFactory.CreateDough();this.sauce = pizzaIngredientFactory.CreateSauce();System.out.println("出海打鱼~~~~");System.out.println("面团:" + dough.destation());System.out.println("酱料:" + sauce.destation());}}//海鲜披萨class SeafoodPizza extends Pizza{PizzaIngredientFactory pizzaIngredientFactory;public SeafoodPizza(PizzaIngredientFactory pizzaIngredientFactory) {name = "海鲜披萨";this.pizzaIngredientFactory = pizzaIngredientFactory;IngredientFactoryName = pizzaIngredientFactory.getName();}void prepare() {this.dough = pizzaIngredientFactory.CreateDough();this.sauce = pizzaIngredientFactory.CreateSauce();System.out.println("出海打鱼~~~~");System.out.println("面团:" + dough.destation());System.out.println("酱料:" + sauce.destation());}}//芝士披萨class CheesePizza extends Pizza{PizzaIngredientFactory pizzaIngredientFactory;public CheesePizza(PizzaIngredientFactory pizzaIngredientFactory) {name = "芝士披萨";this.pizzaIngredientFactory = pizzaIngredientFactory;IngredientFactoryName = pizzaIngredientFactory.getName();}void cut() {System.out.println("切片,铺上芝士");}@Overridevoid prepare() {// TODO Auto-generated method stubthis.dough = pizzaIngredientFactory.CreateDough();this.sauce = pizzaIngredientFactory.CreateSauce();System.out.println("面团:" + dough.destation());System.out.println("酱料:" + sauce.destation());}}// 火箭披萨class RocketPizza extends Pizza{PizzaIngredientFactory pizzaIngredientFactory;public RocketPizza(PizzaIngredientFactory pizzaIngredientFactory) {name = "火箭披萨";this.pizzaIngredientFactory = pizzaIngredientFactory;IngredientFactoryName = pizzaIngredientFactory.getName();}void box() {System.out.println("火箭打包----发射!");}@Overridevoid prepare() {// TODO Auto-generated method stubthis.dough = pizzaIngredientFactory.CreateDough();this.sauce = pizzaIngredientFactory.CreateSauce();System.out.println("面团:" + dough.destation());System.out.println("酱料:" + sauce.destation());}}//============ 披萨工厂相关 =================// 工厂抽象类abstract class PizzaStore{String StoreName;public Pizza OrderPizza(String type){Pizza pizza = CreatePizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();pizza.lable();return pizza;}public abstract Pizza CreatePizza(String type);}// 杭州工厂class HanzhouPizzaStore extends PizzaStore{public HanzhouPizzaStore() {// TODO Auto-generated constructor stubStoreName = "杭州分店";}@Overridepublic Pizza CreatePizza(String type) {// TODO Auto-generated method stubHanzhouPizzaIngredientFactory hanzhouPizzaIngredientFactory = new HanzhouPizzaIngredientFactory();Pizza pizza=null;switch(type){case "海鲜披萨":pizza = new SeafoodPizza(hanzhouPizzaIngredientFactory);break;case "芝士披萨":pizza=new CheesePizza(hanzhouPizzaIngredientFactory);break;default :pizza = new NormolPizza(hanzhouPizzaIngredientFactory);System.out.println("做不出来,用普通披萨代替");break;}pizza.name += " 杭州出品";return pizza;}}// 福建工厂class FujianPizzaStore extends PizzaStore{public FujianPizzaStore() {// TODO Auto-generated constructor stubStoreName = "福建分店";}@Overridepublic Pizza CreatePizza(String type) {// TODO Auto-generated method stubFujianPizzaIngredientFactory fujianPizzaIngredientFactory = new FujianPizzaIngredientFactory();Pizza pizza=null;switch(type){case "海鲜披萨":pizza = new SeafoodPizza(fujianPizzaIngredientFactory);break;case "火箭披萨":pizza=new RocketPizza(fujianPizzaIngredientFactory);break;default :pizza = new NormolPizza(fujianPizzaIngredientFactory);System.out.println("做不出来,用普通披萨代替");break;}pizza.name += " 福建出品";return pizza;}}public class main {static public void main(String[] args) {HanzhouPizzaStore pizzaStore1 = new HanzhouPizzaStore();pizzaStore1.OrderPizza("海鲜披萨");pizzaStore1.OrderPizza("芝士披萨");pizzaStore1.OrderPizza("火箭披萨");FujianPizzaStore pizzaStore2 = new FujianPizzaStore();pizzaStore2.OrderPizza("海鲜披萨");pizzaStore2.OrderPizza("芝士披萨");pizzaStore2.OrderPizza("火箭披萨");}}
