工厂方法模式(Factory Pattern):定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把例化推迟到子类
实例:PIZZA分店需制作不同类型的PIZZA,但制作过程要保持总店的规则
代码:
JAVA
- import java.util.ArrayList;
- abstract class Pizza
- {
- String name;
- String dough;
- String sauce;
- ArrayList toppings=new ArrayList();
- void prepare()
- {
- System.out.println("Preparing "+name);
- System.out.println("Tossing dough...");
- System.out.println("Adding sauce...");
- System.out.println("Adding toppings :");
- for(int i=0;i<toppings.size();i++)
- {
- System.out.println(" "+toppings.get(i));
- }
- }
- void bake()
- {
- System.out.println("Bake for 25 minutes at 350");
- }
- void cut()
- {
- System.out.println("Cutting the pizza into diagonal slices");
- }
- void box()
- {
- System.out.println("Place pizza in offical PizzaStore box");
- }
- public String getName()
- {
- return name;
- }
- }
- class NYStyleCheesePizza extends Pizza
- {
- public NYStyleCheesePizza()
- {
- name="NY style Sauce and Cheese Pizza";
- dough="Thin Crust Dough";
- sauce="Marinara Sauce";
- toppings.add("Grated Reggiano Cheese");
- }
- }
- class NYStyleClamPizza extends Pizza
- {
- public NYStyleClamPizza()
- {
- name = "NY Style Clam Pizza";
- dough = "Thin Crust Dough";
- sauce = "Marinara Sauce";
- toppings.add("Grated Reggiano Cheese");
- toppings.add("Fresh Clams from Long Island Sound");
- }
- }
- class NYStylePepperoniPizza extends Pizza
- {
- public NYStylePepperoniPizza()
- {
- name = "NY Style Pepperoni Pizza";
- dough = "Thin Crust Dough";
- sauce = "Marinara Sauce";
- toppings.add("Grated Reggiano Cheese");
- toppings.add("Sliced Pepperoni");
- toppings.add("Garlic");
- toppings.add("Onion");
- toppings.add("Mushrooms");
- toppings.add("Red Pepper");
- }
- }
- class NYStyleVeggiePizza extends Pizza
- {
- public NYStyleVeggiePizza()
- {
- name = "NY Style Veggie Pizza";
- dough = "Thin Crust Dough";
- sauce = "Marinara Sauce";
- toppings.add("Grated Reggiano Cheese");
- toppings.add("Garlic");
- toppings.add("Onion");
- toppings.add("Mushrooms");
- toppings.add("Red Pepper");
- }
- }
- class ChicagoStyleCheesePizza extends Pizza
- {
- public ChicagoStyleCheesePizza()
- {
- name="Chicago Style Deep Dish Cheese Pizza";
- dough="Extar Thick Crust Dough";
- sauce="Plum Tomato Sauce";
- toppings.add("Shredded Mozzarella Cheese");
- }
- void cut()
- {
- System.out.println("Cutting the pizza into square slices");
- }
- }
- class ChicagoStyleClamPizza extends Pizza
- {
- public ChicagoStyleClamPizza()
- {
- name = "Chicago Style Clam Pizza";
- dough = "Extra Thick Crust Dough";
- sauce = "Plum Tomato Sauce";
- toppings.add("Shredded Mozzarella Cheese");
- toppings.add("Frozen Clams from Chesapeake Bay");
- }
- void cut()
- {
- System.out.println("Cutting the pizza into square slices");
- }
- }
- class ChicagoStylePepperoniPizza extends Pizza
- {
- public ChicagoStylePepperoniPizza()
- {
- name = "Chicago Style Pepperoni Pizza";
- dough = "Extra Thick Crust Dough";
- sauce = "Plum Tomato Sauce";
- toppings.add("Shredded Mozzarella Cheese");
- toppings.add("Black Olives");
- toppings.add("Spinach");
- toppings.add("Eggplant");
- toppings.add("Sliced Pepperoni");
- }
- void cut()
- {
- System.out.println("Cutting the pizza into square slices");
- }
- }
- class ChicagoStyleVeggiePizza extends Pizza
- {
- public ChicagoStyleVeggiePizza()
- {
- name = "Chicago Deep Dish Veggie Pizza";
- dough = "Extra Thick Crust Dough";
- sauce = "Plum Tomato Sauce";
- toppings.add("Shredded Mozzarella Cheese");
- toppings.add("Black Olives");
- toppings.add("Spinach");
- toppings.add("Eggplant");
- }
- void cut()
- {
- System.out.println("Cutting the pizza into square slices");
- }
- }
- abstract class PizzaStore
- {
- public Pizza orderPizza(String type)
- {
- Pizza pizza;
- pizza = createPizza(type);
- pizza.prepare();
- pizza.bake();
- pizza.cut();
- pizza.box();
- return pizza;
- }
- abstract Pizza createPizza(String type);
- }
- class NYPizzaStore extends PizzaStore
- {
- Pizza createPizza(String item)
- {
- if (item.equals("cheese"))
- {
- return new NYStyleCheesePizza();
- }
- else if (item.equals("veggie"))
- {
- return new NYStyleVeggiePizza();
- }
- else if (item.equals("clam"))
- {
- return new NYStyleClamPizza();
- }
- else if (item.equals("pepperoni"))
- {
- return new NYStylePepperoniPizza();
- }
- else
- return null;
- }
- }
- class ChicagoPizzaStore extends PizzaStore
- {
- Pizza createPizza(String item)
- {
- if (item.equals("cheese"))
- {
- return new ChicagoStyleCheesePizza();
- }
- else if (item.equals("veggie"))
- {
- return new ChicagoStyleVeggiePizza();
- }
- else if (item.equals("clam"))
- {
- return new ChicagoStyleClamPizza();
- }
- else if(item.equals("pepperoni"))
- {
- return new ChicagoStylePepperoniPizza();
- }
- else
- return null;
- }
- }
- public class PizzaTestDrive
- {
- public static void main(String Args[])
- {
- PizzaStore nyStore = new NYPizzaStore();
- Pizza pizza = nyStore.orderPizza("cheese");
- System.out.println("Ethan oreded a " + pizza.getName() + "/n");
- PizzaStore chicagoStore = new ChicagoPizzaStore();
- pizza = chicagoStore.orderPizza("cheese");
- System.out.print("Joel orddered a " + pizza.getName() + "/n");
- }
- }
C++
- // PizzaTestDrive.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- using namespace std;
- class Pizza
- {
- protected:
- char* name;
- char* dough;
- char* sauce;
- vector<char *> toppings;
- public:
- char* getName()
- {
- return name;
- }
- virtual void prepare()
- {
- cout<<"Preparing "<<name<<'/n';
- cout<<"Tossing dough..."<<'/n';
- cout<<"Adding sauce..."<<'/n';
- cout<<"Adding toppings :"<<'/n';
- for(int i=0;i<(int)toppings.size();i++)
- {
- cout<<" "<<toppings.at(i)<<'/n';
- }
- }
- virtual void bake()
- {
- cout<<"Bake for 25 minutes at 350/n";
- }
- virtual void cut()
- {
- cout<<"Cutting the pizza into diagonal slices/n";
- }
- virtual void box()
- {
- cout<<"Place pizza in offical PizzaStore box/n";
- }
- };
- class NYStyleCheesePizza :public Pizza
- {
- public:
- NYStyleCheesePizza()
- {
- name="NY style Sauce and Cheese Pizza";
- dough="Thin Crust Dough";
- sauce="Marinara Sauce";
- toppings.push_back("Grated Reggiano Cheese");
- }
- };
- class NYStyleVeggiePizza :public Pizza
- {
- public:
- NYStyleVeggiePizza()
- {
- name = "NY Style Veggie Pizza";
- dough = "Thin Crust Dough";
- sauce = "Marinara Sauce";
- toppings.push_back("Grated Reggiano Cheese");
- toppings.push_back("Garlic");
- toppings.push_back("Onion");
- toppings.push_back("Mushrooms");
- toppings.push_back("Red Pepper");
- }
- };
- class NYStyleClamPizza :public Pizza
- {
- public:
- NYStyleClamPizza()
- {
- name = "NY Style Clam Pizza";
- dough = "Thin Crust Dough";
- sauce = "Marinara Sauce";
- toppings.push_back("Grated Reggiano Cheese");
- toppings.push_back("Fresh Clams from Long Island Sound");
- }
- };
- class NYStylePepperoniPizza :public Pizza
- {
- public:
- NYStylePepperoniPizza()
- {
- name = "NY Style Pepperoni Pizza";
- dough = "Thin Crust Dough";
- sauce = "Marinara Sauce";
- toppings.push_back("Grated Reggiano Cheese");
- toppings.push_back("Sliced Pepperoni");
- toppings.push_back("Garlic");
- toppings.push_back("Onion");
- toppings.push_back("Mushrooms");
- toppings.push_back("Red Pepper");
- }
- };
- class ChicagoStyleCheesePizza :public Pizza
- {
- public:
- ChicagoStyleCheesePizza()
- {
- name="Chicago Style Deep Dish Cheese Pizza";
- dough="Extar Thick Crust Dough";
- sauce="Plum Tomato Sauce";
- toppings.push_back("Shredded Mozzarella Cheese");
- }
- void cut()
- {
- cout<<"Cutting the pizza into square slices/n";
- }
- };
- class ChicagoStyleClamPizza :public Pizza
- {
- public:
- ChicagoStyleClamPizza()
- {
- name = "Chicago Style Clam Pizza";
- dough = "Extra Thick Crust Dough";
- sauce = "Plum Tomato Sauce";
- toppings.push_back("Shredded Mozzarella Cheese");
- toppings.push_back("Frozen Clams from Chesapeake Bay");
- }
- void cut()
- {
- cout<<"Cutting the pizza into square slices/n";
- }
- };
- class ChicagoStylePepperoniPizza :public Pizza
- {
- public:
- ChicagoStylePepperoniPizza()
- {
- name = "Chicago Style Pepperoni Pizza";
- dough = "Extra Thick Crust Dough";
- sauce = "Plum Tomato Sauce";
- toppings.push_back("Shredded Mozzarella Cheese");
- toppings.push_back("Black Olives");
- toppings.push_back("Spinach");
- toppings.push_back("Eggplant");
- toppings.push_back("Sliced Pepperoni");
- }
- void cut()
- {
- cout<<"Cutting the pizza into square slices/n";
- }
- };
- class ChicagoStyleVeggiePizza :public Pizza
- {
- public:
- ChicagoStyleVeggiePizza()
- {
- name = "Chicago Deep Dish Veggie Pizza";
- dough = "Extra Thick Crust Dough";
- sauce = "Plum Tomato Sauce";
- toppings.push_back("Shredded Mozzarella Cheese");
- toppings.push_back("Black Olives");
- toppings.push_back("Spinach");
- toppings.push_back("Eggplant");
- }
- void cut()
- {
- cout<<"Cutting the pizza into square slices/n";
- }
- };
- class PizzaStore
- {
- public:
- Pizza* orderPizza(char* type)
- {
- Pizza* pizza;
- pizza = createPizza(type);
- pizza->prepare();
- pizza->bake();
- pizza->cut();
- pizza->box();
- return pizza;
- }
- virtual Pizza* createPizza(char* type)=0;
- };
- class NYPizzaStore :public PizzaStore
- {
- Pizza* createPizza(char* item)
- {
- if (strcmp(item,"cheese")==0)
- {
- return new NYStyleCheesePizza();
- }
- else if (strcmp(item,"veggie")==0)
- {
- return new NYStyleVeggiePizza();
- }
- else if (strcmp(item,"clam")==0)
- {
- return new NYStyleClamPizza();
- }
- else if (strcmp(item,"pepperoni")==0)
- {
- return new NYStylePepperoniPizza();
- }
- else
- return NULL;
- }
- };
- class ChicagoPizzaStore :public PizzaStore
- {
- Pizza* createPizza(char* item)
- {
- if (strcmp(item,"cheese")==0)
- {
- return new ChicagoStyleCheesePizza();
- }
- else if (strcmp(item,"veggie")==0)
- {
- return new ChicagoStyleVeggiePizza();
- }
- else if (strcmp(item,"clam")==0)
- {
- return new ChicagoStyleClamPizza();
- }
- else if (strcmp(item,"pepperoni")==0)
- {
- return new ChicagoStylePepperoniPizza();
- }
- else
- return NULL;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- PizzaStore* nyStore = new NYPizzaStore();
- Pizza *pizza = nyStore->orderPizza("cheese");
- cout<<"Ethan oreded a " << pizza->getName() <<"/n";
- PizzaStore *chicagoStore = new ChicagoPizzaStore();
- pizza = chicagoStore->orderPizza("cheese");
- cout<<"Joel orddered a " << pizza->getName() << "/n";
- return 0;
- }
结果: