工厂方法模式(Factory Pattern)

工厂方法模式(Factory Pattern):定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把例化推迟到子类

 

实例:PIZZA分店需制作不同类型的PIZZA,但制作过程要保持总店的规则

 

代码:

JAVA

 

  1. import java.util.ArrayList;
  2. abstract class Pizza
  3. {
  4.     String name;
  5.     String dough;
  6.     String sauce;
  7.     ArrayList toppings=new ArrayList();
  8.     void prepare()
  9.     {
  10.         System.out.println("Preparing "+name);
  11.         System.out.println("Tossing dough...");
  12.         System.out.println("Adding sauce...");
  13.         System.out.println("Adding toppings :");
  14.         for(int i=0;i<toppings.size();i++)
  15.         {
  16.             System.out.println("   "+toppings.get(i));
  17.         }
  18.     }
  19.     void bake()
  20.     {
  21.         System.out.println("Bake for 25 minutes at 350");
  22.     }
  23.     void cut()
  24.     {
  25.         System.out.println("Cutting the pizza into diagonal slices");
  26.     }
  27.     void box()
  28.     {
  29.         System.out.println("Place pizza in offical PizzaStore box");
  30.     }
  31.     public String getName()
  32.     {
  33.         return name;
  34.     }
  35. }
  36. class NYStyleCheesePizza extends Pizza
  37. {
  38.     public NYStyleCheesePizza()
  39.     {
  40.         name="NY style Sauce and Cheese Pizza";
  41.         dough="Thin Crust Dough";
  42.         sauce="Marinara Sauce";
  43.         toppings.add("Grated Reggiano Cheese");
  44.     }
  45. }
  46. class NYStyleClamPizza extends Pizza
  47. {
  48.     public NYStyleClamPizza()
  49.     {
  50.         name = "NY Style Clam Pizza";
  51.         dough = "Thin Crust Dough";
  52.         sauce = "Marinara Sauce";
  53.         toppings.add("Grated Reggiano Cheese");
  54.         toppings.add("Fresh Clams from Long Island Sound");
  55.     }
  56. }
  57. class NYStylePepperoniPizza extends Pizza
  58. {
  59.     public NYStylePepperoniPizza()
  60.     {
  61.         name = "NY Style Pepperoni Pizza";
  62.         dough = "Thin Crust Dough";
  63.         sauce = "Marinara Sauce";
  64.         toppings.add("Grated Reggiano Cheese");
  65.         toppings.add("Sliced Pepperoni");
  66.         toppings.add("Garlic");
  67.         toppings.add("Onion");
  68.         toppings.add("Mushrooms");
  69.         toppings.add("Red Pepper");
  70.     }
  71. }
  72. class NYStyleVeggiePizza extends Pizza
  73. {
  74.     public NYStyleVeggiePizza()
  75.     {
  76.         name = "NY Style Veggie Pizza";
  77.         dough = "Thin Crust Dough";
  78.         sauce = "Marinara Sauce";
  79.         toppings.add("Grated Reggiano Cheese");
  80.         toppings.add("Garlic");
  81.         toppings.add("Onion");
  82.         toppings.add("Mushrooms");
  83.         toppings.add("Red Pepper");
  84.     }
  85. }
  86. class ChicagoStyleCheesePizza extends Pizza
  87. {
  88.     public ChicagoStyleCheesePizza()
  89.     {
  90.         name="Chicago Style Deep Dish Cheese Pizza";
  91.         dough="Extar Thick Crust Dough";
  92.         sauce="Plum Tomato Sauce";
  93.         toppings.add("Shredded Mozzarella Cheese");
  94.     }
  95.     void cut()
  96.     {
  97.         System.out.println("Cutting the pizza into square slices");
  98.     }
  99. }
  100. class ChicagoStyleClamPizza extends Pizza
  101. {
  102.     public ChicagoStyleClamPizza()
  103.     {
  104.         name = "Chicago Style Clam Pizza";
  105.         dough = "Extra Thick Crust Dough";
  106.         sauce = "Plum Tomato Sauce";
  107.         toppings.add("Shredded Mozzarella Cheese");
  108.         toppings.add("Frozen Clams from Chesapeake Bay");
  109.     }
  110.     void cut()
  111.     {
  112.         System.out.println("Cutting the pizza into square slices");
  113.     }
  114. }
  115. class ChicagoStylePepperoniPizza extends Pizza
  116. {
  117.     public ChicagoStylePepperoniPizza()
  118.     {
  119.         name = "Chicago Style Pepperoni Pizza";
  120.         dough = "Extra Thick Crust Dough";
  121.         sauce = "Plum Tomato Sauce";
  122.         toppings.add("Shredded Mozzarella Cheese");
  123.         toppings.add("Black Olives");
  124.         toppings.add("Spinach");
  125.         toppings.add("Eggplant");
  126.         toppings.add("Sliced Pepperoni");
  127.     }
  128.     void cut()
  129.     {
  130.         System.out.println("Cutting the pizza into square slices");
  131.     }
  132. }
  133. class ChicagoStyleVeggiePizza extends Pizza
  134. {
  135.     public ChicagoStyleVeggiePizza()
  136.     {
  137.         name = "Chicago Deep Dish Veggie Pizza";
  138.         dough = "Extra Thick Crust Dough";
  139.         sauce = "Plum Tomato Sauce";
  140.         toppings.add("Shredded Mozzarella Cheese");
  141.         toppings.add("Black Olives");
  142.         toppings.add("Spinach");
  143.         toppings.add("Eggplant");
  144.     }
  145.     void cut()
  146.     {
  147.         System.out.println("Cutting the pizza into square slices");
  148.     }
  149. }
  150. abstract class PizzaStore
  151. {
  152.     public Pizza orderPizza(String type)
  153.     {
  154.         Pizza pizza;
  155.         pizza = createPizza(type);
  156.         pizza.prepare();
  157.         pizza.bake();
  158.         pizza.cut();
  159.         pizza.box();
  160.         return pizza;
  161.     }
  162.      abstract Pizza createPizza(String type);
  163. }
  164. class NYPizzaStore extends PizzaStore
  165. {
  166.     Pizza createPizza(String item)
  167.     {
  168.         if (item.equals("cheese"))
  169.         {
  170.             return new NYStyleCheesePizza();
  171.         }
  172.         else if (item.equals("veggie"))
  173.         {
  174.             return new NYStyleVeggiePizza();
  175.         }
  176.         else if (item.equals("clam"))
  177.         {
  178.             return new NYStyleClamPizza();
  179.         }
  180.         else if (item.equals("pepperoni"))
  181.         {
  182.             return new NYStylePepperoniPizza();
  183.         }
  184.         else
  185.             return null;
  186.     }
  187. }
  188. class ChicagoPizzaStore extends PizzaStore
  189. {
  190.     Pizza createPizza(String item)
  191.     {
  192.         if (item.equals("cheese"))
  193.         {
  194.             return new ChicagoStyleCheesePizza();
  195.         }
  196.         else if (item.equals("veggie"))
  197.         {
  198.             return new ChicagoStyleVeggiePizza();
  199.         }
  200.         else if (item.equals("clam"))
  201.         {
  202.             return new ChicagoStyleClamPizza();
  203.         }
  204.         else if(item.equals("pepperoni"))
  205.         {
  206.             return new ChicagoStylePepperoniPizza();
  207.         }
  208.         else
  209.             return null;
  210.     }
  211. }
  212. public class PizzaTestDrive
  213. {
  214.     
  215.     public static void main(String Args[])
  216.     {
  217.         PizzaStore nyStore = new NYPizzaStore();
  218.         Pizza pizza = nyStore.orderPizza("cheese");
  219.         System.out.println("Ethan oreded a " + pizza.getName() + "/n");
  220.         PizzaStore chicagoStore = new ChicagoPizzaStore();
  221.         pizza = chicagoStore.orderPizza("cheese");
  222.         System.out.print("Joel orddered a " + pizza.getName() + "/n");
  223.         
  224.     
  225.     }
  226. }

C++

  1. // PizzaTestDrive.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <vector>
  6. using namespace std;
  7. class Pizza
  8. {
  9. protected:
  10.     char* name;
  11.     char* dough;
  12.     char* sauce;
  13.     vector<char *> toppings;
  14.     
  15. public:
  16.     char* getName()
  17.     {
  18.         return name;
  19.     }
  20.     virtual void prepare()
  21.     {
  22.         cout<<"Preparing "<<name<<'/n';
  23.         cout<<"Tossing dough..."<<'/n';
  24.         cout<<"Adding sauce..."<<'/n';
  25.         cout<<"Adding toppings :"<<'/n';
  26.         for(int i=0;i<(int)toppings.size();i++)
  27.         {
  28.             cout<<"   "<<toppings.at(i)<<'/n';
  29.         }
  30.     }
  31.     virtual void bake()
  32.     {
  33.         cout<<"Bake for 25 minutes at 350/n";
  34.     }
  35.     virtual void cut()
  36.     {
  37.         cout<<"Cutting the pizza into diagonal slices/n";
  38.     }
  39.     virtual void box()
  40.     {
  41.         cout<<"Place pizza in offical PizzaStore box/n";
  42.     }
  43. };
  44. class NYStyleCheesePizza :public Pizza
  45. {
  46. public:
  47.     NYStyleCheesePizza()
  48.     {
  49.         name="NY style Sauce and Cheese Pizza";
  50.         dough="Thin Crust Dough";
  51.         sauce="Marinara Sauce";
  52.         toppings.push_back("Grated Reggiano Cheese");
  53.     }
  54. };
  55. class NYStyleVeggiePizza :public Pizza
  56. {
  57. public:
  58.     NYStyleVeggiePizza()
  59.     {
  60.         name = "NY Style Veggie Pizza";
  61.         dough = "Thin Crust Dough";
  62.         sauce = "Marinara Sauce";
  63.         toppings.push_back("Grated Reggiano Cheese");
  64.         toppings.push_back("Garlic");
  65.         toppings.push_back("Onion");
  66.         toppings.push_back("Mushrooms");
  67.         toppings.push_back("Red Pepper");
  68.     }
  69. };
  70. class NYStyleClamPizza :public Pizza
  71. {
  72. public:
  73.     NYStyleClamPizza()
  74.     {
  75.         name = "NY Style Clam Pizza";
  76.         dough = "Thin Crust Dough";
  77.         sauce = "Marinara Sauce";
  78.         toppings.push_back("Grated Reggiano Cheese");
  79.         toppings.push_back("Fresh Clams from Long Island Sound");
  80.     }
  81. };
  82. class NYStylePepperoniPizza :public Pizza
  83. {
  84. public:
  85.     NYStylePepperoniPizza()
  86.     {
  87.         name = "NY Style Pepperoni Pizza";
  88.         dough = "Thin Crust Dough";
  89.         sauce = "Marinara Sauce";
  90.         toppings.push_back("Grated Reggiano Cheese");
  91.         toppings.push_back("Sliced Pepperoni");
  92.         toppings.push_back("Garlic");
  93.         toppings.push_back("Onion");
  94.         toppings.push_back("Mushrooms");
  95.         toppings.push_back("Red Pepper");
  96.     }
  97. };
  98. class ChicagoStyleCheesePizza :public Pizza
  99. {
  100. public:
  101.     ChicagoStyleCheesePizza()
  102.     {
  103.         name="Chicago Style Deep Dish Cheese Pizza";
  104.         dough="Extar Thick Crust Dough";
  105.         sauce="Plum Tomato Sauce";
  106.         toppings.push_back("Shredded Mozzarella Cheese");
  107.     }
  108.     void cut()
  109.     {
  110.         cout<<"Cutting the pizza into square slices/n";
  111.     }
  112. };
  113. class ChicagoStyleClamPizza :public Pizza
  114. {
  115. public:
  116.     ChicagoStyleClamPizza()
  117.     {
  118.         name = "Chicago Style Clam Pizza";
  119.         dough = "Extra Thick Crust Dough";
  120.         sauce = "Plum Tomato Sauce";
  121.         toppings.push_back("Shredded Mozzarella Cheese");
  122.         toppings.push_back("Frozen Clams from Chesapeake Bay");
  123.     }
  124.     void cut()
  125.     {
  126.         cout<<"Cutting the pizza into square slices/n";
  127.     }
  128. };
  129. class ChicagoStylePepperoniPizza :public Pizza
  130. {
  131. public:
  132.     ChicagoStylePepperoniPizza()
  133.     {
  134.         name = "Chicago Style Pepperoni Pizza";
  135.         dough = "Extra Thick Crust Dough";
  136.         sauce = "Plum Tomato Sauce";
  137.         toppings.push_back("Shredded Mozzarella Cheese");
  138.         toppings.push_back("Black Olives");
  139.         toppings.push_back("Spinach");
  140.         toppings.push_back("Eggplant");
  141.         toppings.push_back("Sliced Pepperoni");
  142.     }
  143.     void cut()
  144.     {
  145.         cout<<"Cutting the pizza into square slices/n";
  146.     }
  147. };
  148. class ChicagoStyleVeggiePizza :public Pizza
  149. {
  150. public:
  151.     ChicagoStyleVeggiePizza()
  152.     {
  153.         name = "Chicago Deep Dish Veggie Pizza";
  154.         dough = "Extra Thick Crust Dough";
  155.         sauce = "Plum Tomato Sauce";
  156.         toppings.push_back("Shredded Mozzarella Cheese");
  157.         toppings.push_back("Black Olives");
  158.         toppings.push_back("Spinach");
  159.         toppings.push_back("Eggplant");
  160.     }
  161.     void cut()
  162.     {
  163.         cout<<"Cutting the pizza into square slices/n";
  164.     }
  165. };
  166. class PizzaStore
  167. {
  168. public:
  169.     Pizza* orderPizza(char* type)
  170.     {
  171.         Pizza* pizza;
  172.         pizza = createPizza(type);
  173.         pizza->prepare();
  174.         pizza->bake();
  175.         pizza->cut();
  176.         pizza->box();
  177.         return pizza;
  178.     }
  179.      virtual  Pizza* createPizza(char* type)=0;
  180. };
  181. class NYPizzaStore :public PizzaStore
  182. {
  183.     Pizza* createPizza(char* item)
  184.     {
  185.         if (strcmp(item,"cheese")==0)
  186.         {
  187.             return new NYStyleCheesePizza();
  188.         }
  189.         else if (strcmp(item,"veggie")==0)
  190.         {
  191.             return new NYStyleVeggiePizza();
  192.         }
  193.         else if (strcmp(item,"clam")==0)
  194.         {
  195.             return new NYStyleClamPizza();
  196.         }
  197.         else if (strcmp(item,"pepperoni")==0)
  198.         {
  199.             return new NYStylePepperoniPizza();
  200.         }
  201.         else
  202.             return NULL;
  203.     }
  204. };
  205. class ChicagoPizzaStore :public  PizzaStore
  206. {
  207.     Pizza* createPizza(char* item)
  208.     {
  209.         if (strcmp(item,"cheese")==0)
  210.         {
  211.             return new ChicagoStyleCheesePizza();
  212.         }
  213.         else if (strcmp(item,"veggie")==0)
  214.         {
  215.             return new ChicagoStyleVeggiePizza();
  216.         }
  217.         else if (strcmp(item,"clam")==0)
  218.         {
  219.             return new ChicagoStyleClamPizza();
  220.         }
  221.         else if (strcmp(item,"pepperoni")==0)
  222.         {
  223.             return new ChicagoStylePepperoniPizza();
  224.         }
  225.         else
  226.             return NULL;
  227.     }
  228. };
  229. int _tmain(int argc, _TCHAR* argv[])
  230. {
  231.     PizzaStore* nyStore = new NYPizzaStore();
  232.     Pizza *pizza = nyStore->orderPizza("cheese");
  233.     cout<<"Ethan oreded a " << pizza->getName() <<"/n";
  234.     PizzaStore *chicagoStore = new ChicagoPizzaStore();
  235.     pizza = chicagoStore->orderPizza("cheese");
  236.     cout<<"Joel orddered a " << pizza->getName() << "/n";
  237.     return 0;
  238. }

 

结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式,而不必暴露对象创建的逻辑。该模式定义了一个工厂类,它负责创建对象,并将其封装在一个方法中。 在.NET中,工厂模式通常使用静态方法或单例实现,其目的是创建一个对象,而无需揭示创建过程的内部细节。这种方法可以提高代码的可维护性和可复用性,并降低耦合度。 下面是一个简单的工厂模式示例,用于创建不同类型的动物: ```csharp public interface IAnimal { void Speak(); } public class Dog : IAnimal { public void Speak() { Console.WriteLine("Woof!"); } } public class Cat : IAnimal { public void Speak() { Console.WriteLine("Meow!"); } } public class AnimalFactory { public static IAnimal CreateAnimal(string type) { switch (type.ToLower()) { case "dog": return new Dog(); case "cat": return new Cat(); default: throw new ArgumentException("Invalid animal type"); } } } ``` 在上面的示例中,我们首先定义了一个IAnimal接口,它包含了Speak方法。然后,我们定义了两个具体的动物类Dog和Cat,它们实现了IAnimal接口。 最后,我们定义了一个AnimalFactory工厂类,它包含了一个静态方法CreateAnimal。该方法接受一个字符串参数,用于指定要创建的动物类型。根据给定的类型,工厂将创建相应的动物对象并返回。 使用工厂模式,我们可以轻松地创建不同类型的动物对象,而无需为每个对象创建单独的构造函数。此外,我们还可以轻松地添加新的动物类型,而无需修改现有代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值