工厂模式

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

披萨商店实例

pizza.h

  1. //
  2. // Created by Administrator on 2022/1/5.
  3. //
  4. #ifndef PIZZA_H
  5. #define PIZZA_H
  6. #include<iostream>
  7. #include<string>
  8. #include<vector>
  9. enum PizzaType{ cheese = 0, pepperoni, clam, veggie };//Pizaa类型
  10. class Pizza{
  11. public:
  12. Pizza(){
  13. }
  14. virtual ~Pizza(){
  15. }
  16. void prepare(){
  17. std::cout << "准备 " << name << std::endl;
  18. std::cout << "和面团... " << std::endl;
  19. std::cout << "添加酱汁... " << std::endl;
  20. std::cout << "添加配料: " <<std::endl;
  21. for (unsigned int i = 0; i < toppings.size(); ++i)
  22. {
  23. std::cout <<toppings[i];
  24. }
  25. std::cout << std::endl;
  26. }
  27. void bake(){//烘烤
  28. std::cout << "在350度的条件下烘烤25分钟" << std::endl;
  29. }
  30. void cut(){//切片
  31. std::cout << "把披萨切成斜片" << std:: endl;
  32. }
  33. void box(){//包装
  34. std::cout << "把披萨放入正规披萨店的包装盒" << std::endl;
  35. }
  36. std::string getName(){
  37. return name;
  38. }
  39. void setName(std::string temp_name){
  40. name = temp_name;
  41. }
  42. void setdough(std::string temp_dough){
  43. dough = temp_dough;
  44. }
  45. void setSauce(std::string temp_sauce){
  46. sauce = temp_sauce;
  47. }
  48. void setToppings(std::vector<std::string> temp_topp){
  49. toppings.assign(temp_topp.begin(),temp_topp.end());
  50. }
  51. private:
  52. std::string name;//名称
  53. std::string dough;//面团类型
  54. std::string sauce;//酱料类型
  55. std::vector<std::string> toppings;//一套作料
  56. };
  57. class NYStyleCheesePizza :public Pizza{
  58. public:
  59. NYStyleCheesePizza(){
  60. setName("纽约风味酱汁奶油披萨");
  61. setdough("薄的硬壳面团");
  62. setSauce("番茄酱");
  63. std::vector<std::string > temp_topp;
  64. temp_topp.push_back("磨碎的");
  65. temp_topp.push_back("Reddiano");
  66. temp_topp.push_back("奶油");
  67. setToppings(temp_topp);
  68. }
  69. };
  70. class ChicagoStyleCheesePizza :public Pizza{
  71. public:
  72. ChicagoStyleCheesePizza(){
  73. setName("芝加哥风味的深盘披萨");
  74. setdough("非常薄的硬壳面团");
  75. setSauce("梅子番茄酱");
  76. std::vector<std::string > temp_topp;
  77. temp_topp.push_back("切碎的");
  78. temp_topp.push_back("Mozzarella");
  79. temp_topp.push_back("奶油");
  80. setToppings(temp_topp);
  81. }
  82. void cut(){
  83. std::cout << "把披萨切成方形" << std::endl;//覆盖基类cut函数
  84. }
  85. };
  86. #endif //CLION_PIZZA_H

pizzastore.h

  1. #ifndef PIZZASTORE_H_
  2. #define PIZZASTORE_H_
  3. #include "pizza.h"
  4. class PizzaStore{
  5. public:
  6. Pizza* orderPizza(PizzaType type){
  7. Pizza* temp_pizza;
  8. temp_pizza = createPizza(type);
  9. temp_pizza->prepare();
  10. temp_pizza->bake();
  11. temp_pizza->cut();
  12. temp_pizza->box();
  13. return temp_pizza;
  14. }
  15. virtual Pizza* createPizza(PizzaType type) = 0;
  16. };
  17. class NYPizzaStore :public PizzaStore{
  18. public:
  19. Pizza* createPizza(PizzaType type){
  20. Pizza *pizza = new Pizza;;
  21. switch (type)
  22. {
  23. case cheese:
  24. pizza = new NYStyleCheesePizza();
  25. break;
  26. /*case cheese:
  27. pizza = new NYStyleCheesePizza();
  28. break;
  29. case cheese:
  30. pizza = new NYStyleCheesePizza();
  31. break;*/
  32. default:
  33. std::cout << "There is not this type of pizze" << std::endl;
  34. }
  35. return pizza;
  36. }
  37. };
  38. class ChicagoPizzaStore :public PizzaStore{
  39. public:
  40. Pizza* createPizza(PizzaType type){
  41. Pizza *pizza = new Pizza;
  42. switch (type)
  43. {
  44. case cheese:
  45. pizza = new ChicagoStyleCheesePizza();
  46. break;
  47. /*case cheese:
  48. pizza = new NYStyleCheesePizza();
  49. break;
  50. case cheese:
  51. pizza = new NYStyleCheesePizza();
  52. break;*/
  53. default:
  54. std::cout << "There is not this type of pizze" << std::endl;
  55. }
  56. return pizza;
  57. }
  58. };
  59. #endif

main.cpp

  1. #include<iostream>
  2. #include "pizza.h"
  3. #include "pizzastore.h"
  4. int main()
  5. {
  6. PizzaStore* nyStore = new NYPizzaStore();
  7. PizzaStore* ChicagoStore = new ChicagoPizzaStore();
  8. Pizza* pizza = nyStore->orderPizza(cheese);//小明的订单
  9. std::cout << "make done" << std:: endl;
  10. std::cout << "Ming ordered" << pizza->getName() << std::endl;
  11. std::cout << std::endl;
  12. std::cout << "----------------------------------------" << std::endl;
  13. std::cout << std::endl;
  14. pizza = ChicagoStore->orderPizza(cheese);//小红的订单
  15. std::cout << "制作完毕" << std::endl;
  16. std::cout << "这是小红点的" << pizza->getName() << std::endl;
  17. }

抽象工厂

定义: 抽象工厂提供一个用于创建相关或依赖对象的家族,而不需要明确指定具体的类。

抽象工厂利用对象组合:对象的创建被实现再工厂接口所暴露出来的方法中。

工厂方法使用继承: 把对象的创建委托给子类,子类实现工厂方法来创建对象。