1. 工厂方法模式(Factory Method Pattern)

定义

  • 一个创建产品对象的工厂接口,让子类决定实例化哪一种实例对象通过继承的方式实现应用程序的解耦,让子类决定实例化哪一个具体类型

设计原则

  • 依赖倒置原则

  • 迪米特原则

  • 里氏替换原则

优点

  • 当需要增加一个新的产品时,我们只需要增加一个具体的产品类和与之对应的具体工厂即可,无须修改原有系统

缺点

  • 类的个数容易过多,增加复杂度

  • 增加了系统的抽象性和理解难度

  • 抽象产品只能生产一种产品,此弊端可使用抽象工厂模式解决

使用场合

  • 当子类型可能会有很多,以后需要不断增添不同的子类实现时

  • 当一个系统尚在框架设计阶段,还不知道将来需要实例化哪些具体类时

  • 系统设计之初不需要具体对象的概念(或者说没有具体对象的概念)

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. // 水果基类
  5. // 数据包含两个,一个是价格,一个是水果名字
  6. // 方法有两种,一种是购买,一种是吃,其中吃
  7. class Fruits {
  8. int price;
  9. string name;
  10. public:
  11. Fruits(int prices, string names) : price(prices), name(names) {}
  12. virtual void eat() {}
  13. virtual void buy() {
  14. printf("If you want to buy a(n) %s, you need pays %d dollar\n", name.c_str(), price);
  15. }
  16. };
  17. // 苹果,质量由种子质量,光照,土壤决定
  18. // 苹果总是甜的,水分含量多的
  19. class apple : public Fruits {
  20. public:
  21. apple(int seed, int sunlight, int soil) : Fruits(seed * 15 + sunlight * 5 +
  22. soil * 9, string("apple")) {}
  23. void eat() {
  24. printf("We eat an apple, it is sweet and washy\n");
  25. }
  26. };
  27. // 葡萄,质量由种子质量,光照,土壤,种植密度决定
  28. // 葡萄总是酸的,水分含量多的
  29. class grape : public Fruits {
  30. public:
  31. grape(int seed, int sunlight, int soil, int density) : Fruits(seed * 9 +
  32. sunlight * 12 + soil * 6 - density * 6, string("grape")) {}
  33. void eat() {
  34. printf("We eat a grape, it is sour and washy\n");
  35. }
  36. };
  37. // 香蕉,质量由种子质量,光照,土壤,温度,湿度决定
  38. // 香蕉总是甜的,水分含量少的
  39. class banana : public Fruits {
  40. public:
  41. banana(int seed, int sunlight, int soil, int temperature, int humidity) :
  42. Fruits(seed * 21 + sunlight * 7 + soil * 15 + temperature * 18 + humidity * 9,
  43. string("banana")) {}
  44. void eat() {
  45. printf("We eat a banana, it is sweet and dry\n");
  46. }
  47. };
  48. // 梨,质量由种子质量,光照,土壤决定
  49. // 梨是否甜或者水分含量多取决于他的甜度与生长时间
  50. class pear : public Fruits {
  51. int sweet;
  52. int passtime;
  53. public:
  54. pear(int seed, int sunlight, int soil) : Fruits(seed * 21 + sunlight * 9 +
  55. soil * 12, string("pear")), sweet(sunlight * 6 + soil * 3), passtime(max(sunlight,
  56. soil) - min(sunlight, soil)) {}
  57. void eat() {
  58. printf("We eat a pear, it is %s and %s\n", passtime >= 3 && sweet >= 697 ? "sweet" : "tasteless", passtime <= 6 ? "washy" : "dry");
  59. }
  60. };
  61. // 工厂屏蔽对应水果类的细节,让用户总是以水果基类得到一个水果,而屏蔽了创建以及中间的实现细节
  62. // 当某个水果工厂生产流程需要改变时,只需修改对应的工厂即可 【可维护】
  63. // 新的水果只需要增加新的工厂即可 【可扩展】
  64. // 苹果制造工厂,生成一个苹果
  65. class AppleFactory {
  66. public:
  67. Fruits* create() {
  68. return new apple(rand() % 30, rand() % 150, rand() % 97);
  69. }
  70. };
  71. // 葡萄制造工厂,生成一颗葡萄
  72. class GrapeFactory {
  73. public:
  74. Fruits* create() {
  75. return new grape(rand() % 30, rand() % 150, rand() % 97, rand() % 16);
  76. }
  77. };
  78. // 香蕉制造工厂,生成一根香蕉
  79. class BananaFactory {
  80. public:
  81. Fruits* create() {
  82. return new banana(rand() % 30, rand() % 150, rand() % 97, rand() % 42,
  83. rand() % 100);
  84. }
  85. };
  86. // 梨制造工厂,生成一个梨
  87. class PearFactory {
  88. public:
  89. Fruits* create() {
  90. return new pear(rand() % 30, rand() % 150, rand() % 97);
  91. }
  92. };
  93. int main()
  94. {
  95. srand(time(NULL));
  96. AppleFactory applefactory = *(new AppleFactory());
  97. Fruits *apples = applefactory.create();
  98. GrapeFactory grapefactory = *(new GrapeFactory());
  99. Fruits *grapse = grapefactory.create();
  100. BananaFactory bananafactory = *(new BananaFactory());
  101. Fruits* banana = bananafactory.create();
  102. PearFactory pearfactory = *(new PearFactory());
  103. Fruits* pear = pearfactory.create();
  104. apples->buy();
  105. grapse->buy();
  106. apples->eat();
  107. grapse->eat();
  108. pear->buy();
  109. banana->buy();
  110. banana->eat();
  111. pear->eat();
  112. return 0;
  113. }

上述代码运行结果:

  1. If you want to buy a(n) apple, you need pays 831 dollar
  2. If you want to buy a(n) grape, you need pays 957 dollar
  3. We eat an apple, it is sweet and washy
  4. We eat a grape, it is sour and washy
  5. If you want to buy a(n) pear, you need pays 2250 dollar
  6. If you want to buy a(n) banana, you need pays 2024 dollar
  7. We eat a banana, it is sweet and dry
  8. We eat a pear, it is sweet and dry

2. 抽象工厂模式

  • 工厂方法模式只考虑生产同等级的产品,但是在现实生活中许多工厂是综合型的工厂
  • 定义:提供一个接口,用于创建相关或者依赖对象的家族,而不需要指定具体的实现类

设计原则

  • “开-闭”原则;多用对象组合,少用继承(不是不用);针对抽象编程,不针对实现编程;产品对象通过抽象工厂暴露的方法创建,具体工厂创建具体产品

    优点

  • 可以在类的内部对产品族中相关联的多等级产品共同管理,而不必专门引入多个新的类来进行管理

  • 当需要产品族时,抽象工厂可以保证客户端始终只使用同一个产品的产品组
  • 抽象工厂增强了程序的可扩展性,当增加一个新的产品族时,不需要修改原代码,满足开闭原则

缺点

  • 新增新的行为会比较麻烦,因为当添加一个新的产品对象时,需要更加需要更改接口及其下所有子类

抽象工厂模式和工厂方法模式的区别

  • 工厂方法模式通过继承的方式实现应用程序的解耦,而抽象工厂模式则通过对象组合的方式实现应用程序的解耦
  • 工厂方法模式用来创建一个抽象产品,具体工厂实现工厂方法来创建具体产品,而抽象工厂模式用来创建一个产品家族的抽象类型

使用场合

  • 创建产品家族,相关产品集合在一起使用的时候
  • 想要提供一个产品类库,并只想显示其接口而不是实现时
  • 通过组合的方式使用工厂时

转自阿翔-自学

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. // 水果基类
  5. // 数据包含两个,一个是价格,一个是水果名字
  6. // 方法有两种,一种是购买,一种是吃,其中吃
  7. class Fruits {
  8. int price;
  9. string name;
  10. public:
  11. Fruits(int prices, string names) : price(prices), name(names) {}
  12. virtual void eat() {}
  13. virtual void buy() {
  14. printf("If you want to buy a(n) %s, you need pays %d dollar\n", name.c_str(), price);
  15. }
  16. };
  17. // 苹果,质量由种子质量,光照,土壤决定
  18. // 苹果总是甜的,水分含量多的
  19. class apple : public Fruits {
  20. public:
  21. apple(int seed, int sunlight, int soil) : Fruits(seed * 15 + sunlight * 5 +soil * 9, string("apple")) {}
  22. void eat() {
  23. printf("We eat an apple, it is sweet and washy\n");
  24. }
  25. };
  26. // 葡萄,质量由种子质量,光照,土壤,种植密度决定
  27. // 葡萄总是酸的,水分含量多的
  28. class grape : public Fruits {
  29. public:
  30. grape(int seed, int sunlight, int soil, int density) : Fruits(seed * 9 +sunlight * 12 + soil * 6 - density * 6, string("grape")) {}
  31. void eat() {
  32. printf("We eat a grape, it is sour and washy\n");
  33. }
  34. };
  35. // 香蕉,质量由种子质量,光照,土壤,温度,湿度决定
  36. // 香蕉总是甜的,水分含量少的
  37. class banana : public Fruits {
  38. public:
  39. banana(int seed, int sunlight, int soil, int temperature, int humidity) : Fruits(seed * 21 + sunlight * 7 + soil * 15 + temperature * 18 + humidity *
  40. 9, string("banana")) {}
  41. void eat() {
  42. printf("We eat a banana, it is sweet and dry\n");
  43. }
  44. };
  45. // 梨,质量由种子质量,光照,土壤决定
  46. // 梨是否甜或者水分含量多取决于他的甜度与生长时间
  47. class pear : public Fruits {
  48. int sweet;
  49. int passtime;
  50. public:
  51. pear(int seed, int sunlight, int soil) : Fruits(seed * 21 + sunlight * 9 + soil * 12, string("pear")), sweet(sunlight * 6 + soil * 3), passtime(max(sunlight, soil) - min(sunlight, soil)) {}
  52. void eat() {
  53. printf("We eat a pear, it is %s and %s\n", passtime >= 3 && sweet >= 697 ? "sweet" : "tasteless", passtime <= 6 ? "washy" : "dry");
  54. }
  55. };
  56. // 工厂抽象类,相当于所有工厂的接口,所有工厂都要继承这个接口,并实现接口规定的函数
  57. // 带来一个问题就是:如果需要加入一个新的方法,那么会影响所有继承此接口的工厂
  58. class Factory {
  59. public:
  60. virtual Fruits* create() = 0;
  61. };
  62. // 工厂屏蔽对应水果类的细节,让用户总是以水果基类得到一个水果,而屏蔽了创建以及中间的实现细节
  63. // 当某个水果工厂生产流程需要改变时,只需修改对应的工厂即可 【可维护】
  64. // 新的水果只需要增加新的工厂即可 【可扩展】
  65. // 苹果制造工厂,生成一个苹果
  66. class AppleFactory : public Factory {
  67. public:
  68. Fruits* create() {
  69. return new apple(rand() % 30, rand() % 150, rand() % 97);
  70. }
  71. };
  72. // 葡萄制造工厂,生成一颗葡萄
  73. class GrapeFactory : public Factory {
  74. public:
  75. Fruits* create() {
  76. return new grape(rand() % 30, rand() % 150, rand() % 97, rand() % 16);
  77. }
  78. };
  79. // 香蕉制造工厂,生成一根香蕉
  80. class BananaFactory : public Factory {
  81. public:
  82. Fruits* create() {
  83. return new banana(rand() % 30, rand() % 150, rand() % 97, rand() % 42,
  84. rand() % 100);
  85. }
  86. };
  87. // 梨制造工厂,生成一个梨
  88. class PearFactory : public Factory {
  89. public:
  90. Fruits* create() {
  91. return new pear(rand() % 30, rand() % 150, rand() % 97);
  92. }
  93. };
  94. int main()
  95. {
  96. srand(time(NULL));
  97. Factory* applefactory = new AppleFactory();
  98. Fruits* apples = applefactory->create();
  99. Factory* grapefactory = new GrapeFactory();
  100. Fruits* grapse = grapefactory->create();
  101. Factory* bananafactory = new BananaFactory();
  102. Fruits* banana = bananafactory->create();
  103. Factory* pearfactory = new PearFactory();
  104. Fruits* pear = pearfactory->create();
  105. apples->buy();
  106. grapse->buy();
  107. apples->eat();
  108. grapse->eat();
  109. pear->buy();
  110. banana->buy();
  111. banana->eat();
  112. pear->eat();
  113. return 0;
  114. }

运行结果:

  1. If you want to buy a(n) apple, you need pays 1595 dollar
  2. If you want to buy a(n) grape, you need pays 636 dollar
  3. We eat an apple, it is sweet and washy
  4. We eat a grape, it is sour and washy
  5. If you want to buy a(n) pear, you need pays 888 dollar
  6. If you want to buy a(n) banana, you need pays 3130 dollar
  7. We eat a banana, it is sweet and dry
  8. We eat a pear, it is tasteless and dry

3.简单工厂模式

  • 定义:简单工厂模式属于创建型模式又叫做静态工厂方法模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

工厂模式 - 图1

  • Factory:工厂类,简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类的创建产品类的方法可以被外界直接调用,创建所需的产品对象。
  • IProduct:抽象产品类,简单工厂模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。
  • Product:具体产品类,是简单工厂模式的创建目标。