123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #ifndef ABSTRACTFACTORY_H
- #define ABSTRACTFACTORY_H
- #include <string>
- #include "spdlog/spdlog.h"
- class AbstractShipBody
- {
- public:
- virtual std::string getShipBody() = 0;
-
- virtual ~AbstractShipBody() {}
- };
- class ShipBodyWood : public AbstractShipBody
- {
- public:
- std::string getShipBody() override
- {
- return std::string("使用《木材》船体");
- }
- };
- class ShipBodySteel : public AbstractShipBody
- {
- public:
- std::string getShipBody() override
- {
- return std::string("使用《钢铁》船体");
- }
- };
- class ShipBodyCompositeMetal : public AbstractShipBody
- {
- public:
- std::string getShipBody() override
- {
- return std::string("使用《合成金属》船体");
- }
- };
- class AbstractEngine
- {
- public:
- virtual std::string getEngine() = 0;
-
- virtual ~AbstractEngine() {}
- };
- class EngineManual : public AbstractEngine
- {
- public:
- std::string getEngine() override
- {
- return std::string("使用《手动》动力");
- }
- };
- class EngineInternalCombustion : public AbstractEngine
- {
- public:
- std::string getEngine() override
- {
- return std::string("使用《内燃机》动力");
- }
- };
- class EngineNuclearEnergy : public AbstractEngine
- {
- public:
- std::string getEngine() override
- {
- return std::string("使用《核能》动力");
- }
- };
- class AbstractWeapon
- {
- public:
- virtual std::string getWeapon() = 0;
-
- virtual ~AbstractWeapon() {}
- };
- class WeaponGun : public AbstractWeapon
- {
- public:
- std::string getWeapon() override
- {
- return std::string("使用《枪》武器");
- }
- };
- class WeaponRapidFireGun : public AbstractWeapon
- {
- public:
- std::string getWeapon() override
- {
- return std::string("使用《速射炮》武器");
- }
- };
- class WeaponLaser : public AbstractWeapon
- {
- public:
- std::string getWeapon() override
- {
- return std::string("使用《激光》武器");
- }
- };
- class Ship
- {
- public:
- Ship(AbstractShipBody* shipBody, AbstractEngine* engine, AbstractWeapon* weapon)
- : m_shipBody(shipBody), m_engine(engine), m_weapon(weapon)
- {
- }
- ~Ship()
- {
- if (m_shipBody != nullptr)
- {
- delete m_shipBody;
- m_shipBody = nullptr;
- }
- if (m_engine != nullptr)
- {
- delete m_engine;
- m_engine = nullptr;
- }
- if (m_weapon != nullptr)
- {
- delete m_weapon;
- m_weapon = nullptr;
- }
- }
- void showShip()
- {
- SPDLOG_INFO("船体:{}", m_shipBody->getShipBody());
- SPDLOG_INFO("动力:{}", m_engine->getEngine());
- SPDLOG_INFO("武器:{}", m_weapon->getWeapon());
- }
- public:
- AbstractShipBody* m_shipBody = nullptr;
- AbstractEngine* m_engine = nullptr;
- AbstractWeapon* m_weapon = nullptr;
- };
- class AbstractFactory
- {
- public:
- virtual Ship* createShip() = 0;
-
- virtual ~AbstractFactory() {}
- };
- class FactoryBase : public AbstractFactory
- {
- public:
- Ship* createShip() override
- {
- auto ship = new class Ship(new ShipBodyWood(), new EngineManual(), new WeaponGun());
- SPDLOG_INFO("【基础款】船建造完成。");
- return ship;
- }
- };
- class FactoryStandard : public AbstractFactory
- {
- public:
- Ship* createShip() override
- {
- auto ship = new class Ship(new ShipBodySteel(), new EngineInternalCombustion(), new WeaponRapidFireGun());
- SPDLOG_INFO("【标准款】船建造完成。");
- return ship;
- }
- };
- class FactoryUltimate : public AbstractFactory
- {
- public:
- Ship* createShip() override
- {
- auto ship = new class Ship(new ShipBodyCompositeMetal(), new EngineNuclearEnergy(), new WeaponLaser());
- SPDLOG_INFO("【旗舰款】船建造完成。");
- return ship;
- }
- };
- #endif
|