工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。(披萨店生产披萨模拟流程)
工厂方法模式通过让子类决定该创建的对象是什么,来达到将对象的过程封装的目地。
简单工厂和工厂方法之间的差异:
1)简单工厂不是一个设计模式,常被误认为是“工厂模式”。
2)简单工厂把全部的事情,在一个地方都处理完了,然而工厂方法却是创建一个框架,让子类决定要如何实现。
3)简单工厂的做法,可以将对象的创建封装起来,但是简单工厂具备工厂方法的弹性,因为简单工厂不能变更正在创建的产品。
工程名称:Factory 下载目录:http://www.cnblogs.com/jrsmith/admin/Files.aspx ,Factory.zip
1 package com.jyu.store; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 抽象 批萨店 类 7 * @author root 8 * 9 */ 10 public abstract class PizzaStore { 11 12 public Pizza orderPizza(String type){ 13 Pizza pizza; 14 15 pizza = createPizza(type); 16 17 pizza.prepare(); 18 pizza.bake(); 19 pizza.cut(); 20 pizza.box(); 21 22 return pizza; 23 } 24 25 /** 26 * 生产具体类型批萨工厂 27 * @param type 28 * @return 29 */ 30 public abstract Pizza createPizza(String type); 31 }
View Code
1 package com.jyu.newyostore; 2 3 import com.jyu.newyopizza.NYStyleCheesePizza; 4 import com.jyu.newyopizza.NYStyleClamPizza; 5 import com.jyu.newyopizza.NYStylePepperoniPizza; 6 import com.jyu.newyopizza.NYStyleVeggiePizza; 7 import com.jyu.pizza.Pizza; 8 import com.jyu.store.PizzaStore; 9 10 /** 11 * 纽约批萨店 12 * @author root 13 * 14 */ 15 public class NYPizzaStore extends PizzaStore { 16 17 @Override 18 public Pizza createPizza(String type) { 19 20 if(type.equals("cheese")){ 21 return new NYStyleCheesePizza(); 22 }else if(type.equals("veggie")){ 23 return new NYStyleVeggiePizza(); 24 }else if(type.equals("clam")){ 25 return new NYStyleClamPizza(); 26 }else if(type.equals("pepperoni")){ 27 return new NYStylePepperoniPizza(); 28 }else{ 29 return null; 30 } 31 } 32 33 }
View Code
1 package com.jyu.chigagostore; 2 3 import com.jyu.chigagopizza.ChicagoStyleCheesePizza; 4 import com.jyu.chigagopizza.ChicagoStyleVeggiePizza; 5 import com.jyu.pizza.Pizza; 6 import com.jyu.store.PizzaStore; 7 8 /** 9 * 芝加哥批萨店 10 * @author root 11 * 12 */ 13 public class CGPizzaStore extends PizzaStore { 14 15 @Override 16 public Pizza createPizza(String type) { 17 18 if(type.equals("cheese")){ 19 return new ChicagoStyleCheesePizza(); 20 }else if(type.equals("veggie")){ 21 return new ChicagoStyleVeggiePizza(); 22 }else{ 23 return null; 24 } 25 } 26 27 }
1 package com.jyu.pizza; 2 3 import java.util.ArrayList; 4 5 /** 6 * 抽象 批萨类 7 * @author root 8 * 9 */ 10 public abstract class Pizza { 11 12 protected String name; 13 protected String dough; 14 protected String sauce; 15 protected ArrayList toppings = new ArrayList(); 16 17 /** 18 * 基本做法:烘烤,切片,装盒 19 */ 20 public void prepare(){ 21 System.out.println("Preparing " + name); 22 System.out.println("Tossin dough..."); 23 System.out.println("Adding sauce.."); 24 System.out.println("Adding toppings: "); 25 for(int i = 0; i < toppings.size(); i++){ 26 System.out.println(" " + toppings.get(i)); 27 } 28 } 29 30 public void bake(){ 31 System.out.println("Bake for 25 minutes at 350"); 32 } 33 34 public void cut(){ 35 System.out.println("Cutting the pizza into diagonal slices"); 36 } 37 38 public void box(){ 39 System.out.println("Place pizza in offical PizzaStore box"); 40 } 41 42 public String getName(){ 43 return name; 44 } 45 }
View Code
1 package com.jyu.newyopizza; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 纽约Cheese批萨 7 * @author root 8 * 9 */ 10 public class NYStyleCheesePizza extends Pizza { 11 12 @SuppressWarnings("unchecked") 13 public NYStyleCheesePizza(){ 14 name = "NY Style Sauce and Cheese Pizza"; 15 dough = "Thin Crust Dough"; 16 sauce = "Marinara Sauce"; 17 18 toppings.add("Grated Reggiano Cheese"); 19 } 20 }
View Code
1 package com.jyu.newyopizza; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 纽约Clam批萨 7 * @author root 8 * 9 */ 10 public class NYStyleClamPizza extends Pizza { 11 12 @SuppressWarnings("unchecked") 13 public NYStyleClamPizza(){ 14 name = "NY Style ClamPizza"; 15 dough = "NYStyleClamPizza Dough"; 16 sauce = "NYStyleClamPizza Sauce"; 17 18 toppings.add("Grated Reggiano Clam"); 19 } 20 }
View Code
1 package com.jyu.newyopizza; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 纽约Pepperoni批萨 7 * @author root 8 * 9 */ 10 public class NYStylePepperoniPizza extends Pizza { 11 12 @SuppressWarnings("unchecked") 13 public NYStylePepperoniPizza(){ 14 name = "NY Style Pepperoni"; 15 dough = "NYStylePepperoniPizza Dough"; 16 sauce = "NYStylePepperoniPizza Sauce"; 17 18 toppings.add("Grated Reggiano Pepperoni"); 19 } 20 }
View Code
1 package com.jyu.newyopizza; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 纽约Veggie批萨 7 * @author root 8 * 9 */ 10 public class NYStyleVeggiePizza extends Pizza { 11 12 @SuppressWarnings("unchecked") 13 public NYStyleVeggiePizza(){ 14 name = "NY Style VeggiePizza"; 15 dough = "NYStyleVeggiePizza Dough"; 16 sauce = "NYStyleVeggiePizza Sauce"; 17 18 toppings.add("Grated Reggiano Veggie"); 19 } 20 }
View Code
1 package com.jyu.chigagopizza; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 芝加哥Cheese批萨 7 * @author root 8 * 9 */ 10 public class ChicagoStyleCheesePizza extends Pizza { 11 12 @SuppressWarnings("unchecked") 13 public ChicagoStyleCheesePizza(){ 14 name = "Chicago Style Sauce and Cheese Pizza"; 15 dough = "Extra Thick Crust Dough"; 16 sauce = "Plum Tomato Sauce"; 17 18 toppings.add("Shredded Mozzarella Cheese"); 19 } 20 21 @Override 22 public void cut(){ 23 System.out.println("Cutting the pizza into square slices"); 24 } 25 26 }
View Code
1 package com.jyu.chigagopizza; 2 3 import com.jyu.pizza.Pizza; 4 5 /** 6 * 芝加哥Veggie批萨 7 * @author root 8 * 9 */ 10 public class ChicagoStyleVeggiePizza extends Pizza { 11 12 @SuppressWarnings("unchecked") 13 public ChicagoStyleVeggiePizza(){ 14 name = "Chicago Style VeggiePizza"; 15 dough = "ChicagoStyleVeggiePizza Dough"; 16 sauce = "ChicagoStyleVeggiePizza Sauce"; 17 18 toppings.add("Shredded Mozzarella Veggie"); 19 } 20 21 @Override 22 public void cut(){ 23 System.out.println("Cutting the pizza into square slices"); 24 } 25 26 }
1 package com.jyu.test; 2 3 import com.jyu.chigagostore.CGPizzaStore; 4 import com.jyu.newyostore.NYPizzaStore; 5 import com.jyu.pizza.Pizza; 6 import com.jyu.store.PizzaStore; 7 8 public class PizzaTestDrive { 9 10 /** 11 * @param args 12 */ 13 public static void main(String[] args) { 14 15 PizzaStore nyStore = new NYPizzaStore(); 16 PizzaStore chicagoStroe = new CGPizzaStore(); 17 18 Pizza pizza = nyStore.orderPizza("cheese"); 19 System.out.println("Ethan ordered a " + pizza.getName() + "\n"); 20 21 pizza = chicagoStroe.orderPizza("cheese"); 22 System.out.println("Joel ordered a " + pizza.getName() + "\n"); 23 } 24 25 }