AbstractFactory.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef ABSTRACTFACTORY_H
  2. #define ABSTRACTFACTORY_H
  3. /****************** 抽象工厂模式示例 ******************/
  4. /**
  5. * @brief 抽象工厂模式示例,需求如下
  6. * 这里需求是造一艘船,分为三个版本,基础款、标准款、旗舰款,每款的船的船体、动力、武器都不相同。
  7. --------------------------------
  8. | | 基础型 | 标准型 | 旗舰型 |
  9. | --------------------------------
  10. | 船体 | 木头 | 钢铁 | 合成金属 |
  11. | 动力 | 手动 | 内燃机 | 核能 |
  12. | 武器 | 枪 | 速射炮 | 激光 |
  13. --------------------------------
  14. 1. 定义船体、动力、武器的抽象类
  15. 2. 创建一个船类,使用“组合”的方式包含船体、动力、武器的抽象类
  16. 3. 创建一个抽象工厂类,定义造船的接口
  17. */
  18. #include <string>
  19. #include "spdlog/spdlog.h"
  20. /****************** 抽象船体 ******************/
  21. class AbstractShipBody
  22. {
  23. public:
  24. virtual std::string getShipBody() = 0;
  25. /* 定义虚析构函数 */
  26. virtual ~AbstractShipBody() {}
  27. };
  28. class ShipBodyWood : public AbstractShipBody
  29. {
  30. public:
  31. std::string getShipBody() override
  32. {
  33. return std::string("使用《木材》船体");
  34. }
  35. };
  36. class ShipBodySteel : public AbstractShipBody
  37. {
  38. public:
  39. std::string getShipBody() override
  40. {
  41. return std::string("使用《钢铁》船体");
  42. }
  43. };
  44. class ShipBodyCompositeMetal : public AbstractShipBody
  45. {
  46. public:
  47. std::string getShipBody() override
  48. {
  49. return std::string("使用《合成金属》船体");
  50. }
  51. };
  52. /****************** 抽象动力 ******************/
  53. class AbstractEngine
  54. {
  55. public:
  56. virtual std::string getEngine() = 0;
  57. /* 定义虚析构函数 */
  58. virtual ~AbstractEngine() {}
  59. };
  60. class EngineManual : public AbstractEngine
  61. {
  62. public:
  63. std::string getEngine() override
  64. {
  65. return std::string("使用《手动》动力");
  66. }
  67. };
  68. class EngineInternalCombustion : public AbstractEngine
  69. {
  70. public:
  71. std::string getEngine() override
  72. {
  73. return std::string("使用《内燃机》动力");
  74. }
  75. };
  76. class EngineNuclearEnergy : public AbstractEngine
  77. {
  78. public:
  79. std::string getEngine() override
  80. {
  81. return std::string("使用《核能》动力");
  82. }
  83. };
  84. /****************** 抽象武器 ******************/
  85. class AbstractWeapon
  86. {
  87. public:
  88. virtual std::string getWeapon() = 0;
  89. /* 定义虚析构函数 */
  90. virtual ~AbstractWeapon() {}
  91. };
  92. class WeaponGun : public AbstractWeapon
  93. {
  94. public:
  95. std::string getWeapon() override
  96. {
  97. return std::string("使用《枪》武器");
  98. }
  99. };
  100. class WeaponRapidFireGun : public AbstractWeapon
  101. {
  102. public:
  103. std::string getWeapon() override
  104. {
  105. return std::string("使用《速射炮》武器");
  106. }
  107. };
  108. class WeaponLaser : public AbstractWeapon
  109. {
  110. public:
  111. std::string getWeapon() override
  112. {
  113. return std::string("使用《激光》武器");
  114. }
  115. };
  116. /****************** 轮船 ******************/
  117. class Ship
  118. {
  119. public:
  120. Ship(AbstractShipBody* shipBody, AbstractEngine* engine, AbstractWeapon* weapon)
  121. : m_shipBody(shipBody), m_engine(engine), m_weapon(weapon)
  122. {
  123. }
  124. ~Ship()
  125. {
  126. if (m_shipBody != nullptr)
  127. {
  128. delete m_shipBody;
  129. m_shipBody = nullptr;
  130. }
  131. if (m_engine != nullptr)
  132. {
  133. delete m_engine;
  134. m_engine = nullptr;
  135. }
  136. if (m_weapon != nullptr)
  137. {
  138. delete m_weapon;
  139. m_weapon = nullptr;
  140. }
  141. }
  142. void showShip()
  143. {
  144. SPDLOG_INFO("船体:{}", m_shipBody->getShipBody());
  145. SPDLOG_INFO("动力:{}", m_engine->getEngine());
  146. SPDLOG_INFO("武器:{}", m_weapon->getWeapon());
  147. }
  148. public:
  149. AbstractShipBody* m_shipBody = nullptr;
  150. AbstractEngine* m_engine = nullptr;
  151. AbstractWeapon* m_weapon = nullptr;
  152. };
  153. /****************** 抽象工厂 ******************/
  154. class AbstractFactory
  155. {
  156. public:
  157. virtual Ship* createShip() = 0;
  158. /* 定义虚析构函数 */
  159. virtual ~AbstractFactory() {}
  160. };
  161. class FactoryBase : public AbstractFactory
  162. {
  163. public:
  164. Ship* createShip() override
  165. {
  166. auto ship = new class Ship(new ShipBodyWood(), new EngineManual(), new WeaponGun());
  167. SPDLOG_INFO("【基础款】船建造完成。");
  168. return ship;
  169. }
  170. };
  171. class FactoryStandard : public AbstractFactory
  172. {
  173. public:
  174. Ship* createShip() override
  175. {
  176. auto ship = new class Ship(new ShipBodySteel(), new EngineInternalCombustion(), new WeaponRapidFireGun());
  177. SPDLOG_INFO("【标准款】船建造完成。");
  178. return ship;
  179. }
  180. };
  181. class FactoryUltimate : public AbstractFactory
  182. {
  183. public:
  184. Ship* createShip() override
  185. {
  186. auto ship = new class Ship(new ShipBodyCompositeMetal(), new EngineNuclearEnergy(), new WeaponLaser());
  187. SPDLOG_INFO("【旗舰款】船建造完成。");
  188. return ship;
  189. }
  190. };
  191. #endif // ABSTRACTFACTORY_H