当前位置:   article > 正文

【设计模式】生成器

【设计模式】生成器

来源:爱编程的大丙

复杂的问题化繁为简,逐个击破。也就是分步骤创建复杂的对象,并允许使用相同代码生成不同类型和形式的对象,这种模式叫做生成器模式(也叫建造者模式)。

生成器模式建议将造船工序的代码从产品类抽取出来并将其放在一个名为生成器的独立类。

海贼船:桑尼类,梅利类,通过字符串代表某个零件

船生成器:每艘船的零部件由对应的生成器类构造完成,抽象类中定义建造海贼船所有零部件的方法,在这个类的子类中需要重写这些虚函数,分别完成桑尼号 和梅利号零件的建造。

在这个生成器类中只要调用build方法,对应的零件就会加载到SunnyShip类的对象m_sunny中,当当船被造好之后就可以通过SunnyShip* getsunny()方法得到桑尼的实例对象

主管类:桑尼号和梅利号分别有三个规格,简约型、标准型、豪华型根据不同的规格,有选择的调用生成器中不同的建造函数,就可以得到最终的成品了。

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<vector>
  5. using namespace std;
  6. class SunnyShip {
  7. public:
  8. //添加零件
  9. void addParts(string name) {
  10. m_parts.push_back(name);
  11. }
  12. void showParts() {
  13. for (const auto& item : m_parts) {
  14. cout << item << " ";
  15. }
  16. cout << endl;
  17. }
  18. private:
  19. vector<string>m_parts;
  20. };
  21. class MerryShip {
  22. public:
  23. //组装
  24. void assemble(string name, string parts) {
  25. m_parts.insert(make_pair(name, parts));
  26. }
  27. void showParts() {
  28. for (const auto& item : m_parts) {
  29. cout << item.first << ": " << item.second << " ";
  30. }
  31. cout << endl;
  32. }
  33. private:
  34. map<string, string>m_parts;
  35. };
  36. //生成器类 定义建造海贼船所有零件的方法,在这个类中需要重写这些虚函数
  37. class ShipBuilder {
  38. public:
  39. virtual void reset() = 0;
  40. virtual void buildBody() = 0;
  41. virtual void buildWeapon() = 0;
  42. virtual void buildEngine() = 0;
  43. virtual void buildInterior() = 0;
  44. virtual ~ShipBuilder() {}
  45. };
  46. //桑尼号生产器
  47. class SunnyBuilder :public ShipBuilder {
  48. public:
  49. SunnyBuilder() {
  50. reset();
  51. }
  52. ~SunnyBuilder() {
  53. if (m_sunny != nullptr) {
  54. delete m_sunny;
  55. m_sunny = nullptr;//删除后将指针置为nullptr
  56. }
  57. }
  58. //提供重置函数,目的是能够使用生成器对象生产多个产品
  59. void reset() override {
  60. if (m_sunny != nullptr) {
  61. delete m_sunny;
  62. }
  63. m_sunny = new SunnyShip;
  64. }
  65. void buildBody() override {
  66. m_sunny->addParts("神树亚当的树干");
  67. }
  68. void buildWeapon() override {
  69. m_sunny->addParts("狮子吼");
  70. }
  71. void buildEngine() override {
  72. m_sunny->addParts("可乐驱动");
  73. }
  74. void buildInterior() override {
  75. m_sunny->addParts("豪华内室精装");
  76. }
  77. SunnyShip* getSunny() {
  78. SunnyShip* ship = m_sunny;
  79. m_sunny = nullptr;
  80. return ship;
  81. }
  82. private:
  83. SunnyShip* m_sunny = nullptr;
  84. };
  85. //梅利号生产器
  86. class MerryBuilder :public ShipBuilder {
  87. public:
  88. MerryBuilder() {
  89. reset();
  90. }
  91. ~MerryBuilder() {
  92. if (m_merry != nullptr) {
  93. delete m_merry;
  94. m_merry = nullptr;//删除后将指针置为nullptr
  95. }
  96. }
  97. //提供重置函数,目的是能够使用生成器对象生产多个产品
  98. void reset() override {
  99. m_merry = new MerryShip;
  100. }
  101. void buildBody() override {
  102. m_merry->assemble("船体","优质木材");
  103. }
  104. void buildWeapon() override {
  105. m_merry->assemble("武器","四门大炮");
  106. }
  107. void buildEngine() override {
  108. m_merry->assemble("动力","蒸汽机");
  109. }
  110. void buildInterior() override {
  111. m_merry->assemble("内室","精修");
  112. }
  113. MerryShip* getMerry() {
  114. MerryShip* ship = m_merry;
  115. m_merry = nullptr;
  116. return ship;
  117. }
  118. private:
  119. MerryShip* m_merry = nullptr;
  120. };
  121. //主管类
  122. class Director {
  123. public:
  124. void setBuilder(ShipBuilder* builder) {
  125. m_builder = builder;
  126. }
  127. //简约型
  128. void buildSimpleShip() {
  129. m_builder->buildBody();
  130. m_builder->buildEngine();
  131. }
  132. //标准型
  133. void buildStandardShip() {
  134. buildSimpleShip();
  135. m_builder->buildWeapon();
  136. }
  137. //豪华型
  138. void buildRegalShip() {
  139. buildStandardShip();
  140. m_builder->buildInterior();
  141. }
  142. private:
  143. ShipBuilder* m_builder = nullptr;
  144. };
  145. //建造桑尼号
  146. void BuilderSunny() {
  147. Director* director = new Director;
  148. SunnyBuilder* builder = new SunnyBuilder;
  149. //简约型
  150. director->setBuilder(builder);
  151. director->buildSimpleShip();
  152. SunnyShip* sunny = builder->getSunny();
  153. sunny->showParts();
  154. delete sunny;
  155. //标准型
  156. builder->reset();
  157. director->setBuilder(builder);
  158. director->buildStandardShip();
  159. sunny = builder->getSunny();
  160. sunny->showParts();
  161. delete sunny;
  162. //豪华型
  163. builder->reset();
  164. director->setBuilder(builder);
  165. director->buildRegalShip();
  166. sunny = builder->getSunny();
  167. sunny->showParts();
  168. delete sunny;
  169. delete builder;
  170. delete director;
  171. }
  172. int main() {
  173. BuilderSunny();
  174. return 0;
  175. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/621209
推荐阅读
相关标签
  

闽ICP备14008679号