0


自学设计模式(简单工厂模式、工厂模式、抽象工厂模式)

使用工厂模式来生产某类对象(代码简化且容易维护,类之间有血缘关系,可以通过工厂类进行生产);

简单工厂模式(用于创建简单对象)

对于简单工厂模式,需要的工厂类只有一个;

在工厂类中的公共成员函数来创建所需对象;

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // 产品父类
  4. class shape{
  5. public:
  6. virtual void cal() = 0;
  7. virtual ~shape(){
  8. }
  9. };
  10. class triangle:public shape{
  11. public:
  12. void cal() override{
  13. cout<<"三角形面积";
  14. }
  15. };
  16. class square:public shape{
  17. public:
  18. void cal() override{
  19. cout<<"正方形面积";
  20. }
  21. };
  22. class cycle:public shape{
  23. public:
  24. void cal() override{
  25. cout<<"圆形面积";
  26. }
  27. };
  28. // 工厂类
  29. enum class Type:char{
  30. triangle ,
  31. square ,
  32. cycle
  33. };
  34. class fac{
  35. public:
  36. shape* creat_kinds(Type type){
  37. shape* ptr = nullptr;
  38. switch(type){
  39. case Type::triangle:
  40. ptr = new triangle;
  41. break;
  42. case Type::square:
  43. ptr = new square;
  44. break;
  45. case Type::cycle:
  46. ptr = new cycle;
  47. break;
  48. }
  49. return ptr;
  50. }
  51. };
  52. int main(){
  53. fac* cur = new fac;
  54. shape* obj = cur->creat_kinds(Type::cycle);
  55. obj->cal();
  56. return 0;
  57. }

工厂模式

简单工厂模式会违反开放封闭原则 在添加类时需要修改子类、枚举类、工厂类、和判断代码

工厂模式通过创立N个工厂类来解决上述问题(对简单工厂模式解耦合),对已经写好的代码无需修改;

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // 产品父类
  4. class shape{
  5. public:
  6. virtual void cal() = 0;
  7. virtual ~shape(){
  8. }
  9. };
  10. class triangle:public shape{
  11. public:
  12. void cal() override{
  13. cout<<"三角形面积"<<endl;
  14. }
  15. };
  16. class square:public shape{
  17. public:
  18. void cal() override{
  19. cout<<"正方形面积"<<endl;
  20. }
  21. };
  22. class cycle:public shape{
  23. public:
  24. void cal() override{
  25. cout<<"圆形面积"<<endl;
  26. }
  27. };
  28. // 工厂类
  29. class fac{
  30. public:
  31. virtual shape* creat_kinds() = 0;
  32. virtual ~fac(){
  33. }
  34. };
  35. class triangle_fac:public fac{
  36. public:
  37. shape* creat_kinds(){
  38. return new triangle;
  39. }
  40. ~triangle_fac(){
  41. cout<<"三角形被析构";
  42. }
  43. };
  44. class square_fac:public fac{
  45. public:
  46. shape* creat_kinds(){
  47. return new square;
  48. }
  49. ~square_fac(){
  50. cout<<"三角形被析构";
  51. }
  52. };
  53. class cycle_fac:public fac{
  54. public:
  55. shape* creat_kinds(){
  56. return new cycle;
  57. }
  58. ~cycle_fac(){
  59. cout<<"三角形被析构";
  60. }
  61. };
  62. int main(){
  63. fac* cur = new cycle_fac;
  64. shape* obj = cur->creat_kinds();
  65. obj->cal();
  66. delete obj;
  67. delete cur;
  68. return 0;
  69. }

抽象工厂模式

通过造船,船分为三个模块,船体、武器、动力,三个模块又分为了简易、标准、旗舰三个版本

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // 三个抽象类 每个抽象类下三个具体派生类
  4. // 抽象工厂类 一个抽象类 三个派生类
  5. class body{
  6. public:
  7. virtual void getbody() = 0;
  8. virtual ~body(){
  9. }
  10. };
  11. class wood_body:public body{
  12. public:
  13. void getbody() override{
  14. cout<<"船的船体为<木头>---";
  15. }
  16. };
  17. class iron_body:public body{
  18. public:
  19. void getbody() override{
  20. cout<<"船的船体为<钢铁>---";
  21. }
  22. };
  23. class mental_body:public body{
  24. public:
  25. void getbody() override{
  26. cout<<"船的船体为<合成金属>---";
  27. }
  28. };
  29. class weapon{
  30. public:
  31. virtual void getweapon() = 0;
  32. virtual ~weapon(){
  33. }
  34. };
  35. class gun_weapon:public weapon{
  36. public:
  37. void getweapon() override{
  38. cout<<"船的武器为<枪>---" ;
  39. }
  40. };
  41. class cannon_weapon:public weapon{
  42. public:
  43. void getweapon() override{
  44. cout<<"船的武器为<炮>---" ;
  45. }
  46. };
  47. class laser_weapon:public weapon{
  48. public:
  49. void getweapon() override{
  50. cout<<"船的武器为<激光>---" ;
  51. }
  52. };
  53. class power{
  54. public:
  55. virtual void getpower() = 0;
  56. virtual ~power(){
  57. }
  58. };
  59. class human_power:public power{
  60. public:
  61. void getpower() override{
  62. cout<<"船的动力为<手动>---";
  63. }
  64. };
  65. class engine_power:public power{
  66. public:
  67. void getpower() override{
  68. cout<<"船的动力为<内燃机>---";
  69. }
  70. };
  71. class nuclear_power:public power{
  72. public:
  73. void getpower() override{
  74. cout<<"船的动力为<核反应堆>---";
  75. }
  76. };
  77. class ship{
  78. private:
  79. body* m_body;
  80. weapon* m_weapon;
  81. power* m_power;
  82. public:
  83. ship(body* o_body , weapon* o_weapon , power* o_power):m_body(o_body) , m_weapon(o_weapon) , m_power(o_power){
  84. m_body->getbody();
  85. m_weapon->getweapon();
  86. m_power->getpower();
  87. }
  88. ~ ship(){
  89. delete m_body;
  90. delete m_weapon;
  91. delete m_power;
  92. }
  93. };
  94. class fac{
  95. public:
  96. virtual ship* get() = 0;
  97. ~ fac(){
  98. }
  99. };
  100. class e_fac:public fac{
  101. public:
  102. ship* get() override{
  103. ship* cur = new ship(new wood_body , new gun_weapon , new human_power);
  104. return cur;
  105. }
  106. };
  107. class m_fac:public fac{
  108. public:
  109. ship* get() override{
  110. ship* cur = new ship(new iron_body , new cannon_weapon , new engine_power);
  111. return cur;
  112. }
  113. };
  114. class h_fac:public fac{
  115. public:
  116. ship* get() override{
  117. ship* cur = new ship(new mental_body , new laser_weapon , new nuclear_power);
  118. return cur;
  119. }
  120. };
  121. int main() {
  122. fac* cur = new h_fac;
  123. ship* res = cur->get();
  124. delete cur;
  125. delete res;
  126. return 0;
  127. }

描述:首先定义三个抽象类,代表船的三个组成部分(船体、武器、动力);每个抽象类下有三个派生类,分别对应初级船、中级船、高级船所用的对应材料。通过ship类将船的三个部分组成起来,一个工厂抽象类fac,通过工厂抽象类的派生类,结合ship类设置三种规格的船。

标签: 设计模式

本文转载自: https://blog.csdn.net/rygy_/article/details/132453990
版权归原作者 逮到647了 所有, 如有侵权,请联系我们删除。

“自学设计模式(简单工厂模式、工厂模式、抽象工厂模式)”的评论:

还没有评论