Browse Source

V0.6
1、新增了设计模式工程

Apple 2 months ago
parent
commit
cf1465bd4b

+ 3 - 2
CMakeLists.txt

@@ -118,7 +118,7 @@ find_package(Qt5 COMPONENTS
 )
 
 include(${CMAKE_SOURCE_DIR}/External/Libraries/Libraries.cmake)
-# include(${CMAKE_SOURCE_DIR}/External_Ex/Library_EX.cmake)
+include(${CMAKE_SOURCE_DIR}/External_Ex/Library_EX.cmake)
 
 
 #=========================================================
@@ -145,10 +145,11 @@ file(GLOB GLOBAL_SRC
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/mqtt)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/http)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/threadPool)
-add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ftp)
+# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ftp)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/OneThread)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/timer)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/time)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/VideoPlayer)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/xlsx)
+add_subdirectory(${CMAKE_SOURCE_DIR}/demo/DesignerPattern)
 

+ 1 - 1
External

@@ -1 +1 @@
-Subproject commit e2dcde16aa6d26fcd0b7b7a078fc78f8e9b28dff
+Subproject commit cc1b2f06472f420a7e0027b9131e3ee86a6ad285

+ 3 - 0
demo/DesignerPattern/AbstractFactory/AbstractFactory.cpp

@@ -0,0 +1,3 @@
+#include "AbstractFactory.h"
+
+

+ 223 - 0
demo/DesignerPattern/AbstractFactory/AbstractFactory.h

@@ -0,0 +1,223 @@
+#ifndef ABSTRACTFACTORY_H
+#define ABSTRACTFACTORY_H
+
+
+
+/****************** 抽象工厂模式示例 ******************/
+
+/**
+ * @brief 抽象工厂模式示例,需求如下
+ * 这里需求是造一艘船,分为三个版本,基础款、标准款、旗舰款,每款的船的船体、动力、武器都不相同。
+--------------------------------
+|       | 基础型 | 标准型 | 旗舰型 |
+| --------------------------------
+| 船体  | 木头   | 钢铁   | 合成金属 |
+| 动力  | 手动   | 内燃机 | 核能    |
+| 武器  | 枪     | 速射炮 | 激光    |
+--------------------------------
+
+    1. 定义船体、动力、武器的抽象类
+    2. 创建一个船类,使用“组合”的方式包含船体、动力、武器的抽象类
+    3. 创建一个抽象工厂类,定义造船的接口
+
+ */
+
+#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 // ABSTRACTFACTORY_H

+ 80 - 0
demo/DesignerPattern/CMakeLists.txt

@@ -0,0 +1,80 @@
+cmake_minimum_required(VERSION 3.5)
+
+set(this_exe DesignerPattern)
+
+
+#包含源文件
+file(GLOB LOCAL_SRC
+    ${CMAKE_SOURCE_DIR}/External/module/Logs/*.cpp
+
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.rc
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
+    ${CMAKE_CURRENT_SOURCE_DIR}/AbstractFactory/*.cpp
+)
+
+
+
+# 生成可执行程序
+
+add_executable(${this_exe}
+    # WIN32
+    ${GLOBAL_SRC}
+    ${LOCAL_SRC} 
+)
+
+
+#添加头文件
+target_include_directories(${this_exe} PRIVATE
+
+    ${CMAKE_CURRENT_SOURCE_DIR}
+    ${CMAKE_SOURCE_DIR}/External/common
+    ${CMAKE_SOURCE_DIR}/External/module
+    ${CMAKE_SOURCE_DIR}/External/module/Logs
+
+    ${CMAKE_CURRENT_SOURCE_DIR}/AbstractFactory
+
+    ${spdlog_INCLUDE_DIR}
+)
+
+target_link_libraries(${this_exe} PRIVATE
+    Qt5::Widgets
+    Qt5::Core
+    Qt5::Network
+    # Qt5::Multimedia
+    # Qt5::Xml
+    # Qt5::Sql
+)
+
+target_link_libraries(${this_exe} PRIVATE 
+    # ${CURL_LIBRARY}
+    ${spdlog_LIBRARY}
+    # ${OpenSSL-1.1.1_LIB_LIBRARY}
+    # CURL::libcurl
+)
+
+if(CMAKE_CXX_COMPILER_VERSION LESS 9.0)
+    target_link_libraries(${this_exe} PRIVATE
+        stdc++fs
+    )
+endif()
+
+# target_link_libraries(${this_exe} PRIVATE
+#     ${CURL_LIBRARY}
+    
+# )
+# message(STATUS "CURL_LIBRARY: ${CURL_LIBRARY}")
+
+
+# if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
+#     target_link_libraries(${this_exe} PRIVATE
+#         # debug spdlogd.lib
+#         # optimized spdlog.lib
+#     )
+# elseif(CMAKE_CXX_COMPILER_ID MATCHES GNU)
+#     target_link_libraries(${this_exe} PRIVATE
+#         # debug 
+#         # optimized ${SM_DLL}
+#     )
+# endif()

+ 21 - 0
demo/DesignerPattern/main.cpp

@@ -0,0 +1,21 @@
+#include "widget.h"
+
+#include <QApplication>
+#include "loginit.h"
+
+#include "AbstractFactory/AbstractFactory.h"
+
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    init_log();
+    
+    /* 建造一艘旗舰战船 */
+    AbstractFactory* factory = new FactoryUltimate();
+    Ship* ship = factory->createShip();
+    ship->showShip();
+
+    delete ship;
+    return a.exec();
+}

+ 31 - 0
demo/DesignerPattern/widget.cpp

@@ -0,0 +1,31 @@
+#include "widget.h"
+#include "./ui_widget.h"
+
+#include "spdlog/spdlog.h"
+
+#include "nlohmann/json.hpp"
+#define nJson nlohmann::json
+
+Widget::Widget(QWidget *parent)
+    : QWidget(parent)
+    , ui(new Ui::Widget)
+{
+    ui->setupUi(this);
+
+
+
+    SPDLOG_INFO("✨✨✨✨✨ Qt Library ✨✨✨✨✨");
+}
+
+Widget::~Widget()
+{
+
+    delete ui;
+}
+
+
+
+
+
+
+

+ 25 - 0
demo/DesignerPattern/widget.h

@@ -0,0 +1,25 @@
+#ifndef WIDGET_H
+#define WIDGET_H
+
+#include <QWidget>
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class Widget; }
+QT_END_NAMESPACE
+
+class Widget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    Widget(QWidget *parent = nullptr);
+    ~Widget();
+
+private slots:
+
+
+private:
+    Ui::Widget *ui;
+
+};
+#endif // WIDGET_H

+ 30 - 0
demo/DesignerPattern/widget.ui

@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Widget</class>
+ <widget class="QWidget" name="Widget">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Widget</string>
+  </property>
+  <widget class="QWidget" name="widget_pBtn" native="true">
+   <property name="geometry">
+    <rect>
+     <x>30</x>
+     <y>30</y>
+     <width>681</width>
+     <height>311</height>
+    </rect>
+   </property>
+   <layout class="QGridLayout" name="gridLayout"/>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 13 - 9
demo/VideoPlayer/CMakeLists.txt

@@ -9,11 +9,12 @@ file(GLOB LOCAL_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/*.rc
     ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
-    ${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer/*.cpp
+    # ${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer/*.cpp
     # ${CMAKE_CURRENT_SOURCE_DIR}/demo/*.cpp
 
-    ${CMAKE_SOURCE_DIR}/External/common/Logs/*.cpp
-    ${CMAKE_SOURCE_DIR}/External/common/ThreadPool/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/Logs/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/ThreadPool/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/VideoPlayer/*.cpp
     
 )
 
@@ -35,15 +36,17 @@ add_executable(${this_exe}
 target_include_directories(${this_exe} PRIVATE
 
     ${CMAKE_CURRENT_SOURCE_DIR}
-    ${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer
+    # ${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer
     # ${CMAKE_CURRENT_SOURCE_DIR}/demo
     ${CMAKE_SOURCE_DIR}/External/common
-    ${CMAKE_SOURCE_DIR}/External/common/FmtLog
-    ${CMAKE_SOURCE_DIR}/External/common/ThreadPool
-    ${CMAKE_SOURCE_DIR}/External/common/RingQueue
+    ${CMAKE_SOURCE_DIR}/External/module
+    ${CMAKE_SOURCE_DIR}/External/module/ThreadPool
+    ${CMAKE_SOURCE_DIR}/External/module/RingQueue
+    ${CMAKE_SOURCE_DIR}/External/module/VideoPlayer
     
     ${CURL_INCLUDE_DIR}
     ${FFMPEG_INCLUDE_DIR}
+    ${spdlog_INCLUDE_DIR}
 )
 
 target_link_libraries(${this_exe} PRIVATE
@@ -56,10 +59,11 @@ target_link_libraries(${this_exe} PRIVATE
 )
 
 target_link_libraries(${this_exe} PRIVATE 
-    fmt::fmt
-    spdlog::spdlog
+    # fmt::fmt
+    # spdlog::spdlog
     ${CURL_LIBRARY}
     ${FFMPEG_LIBRARY}
+    ${spdlog_LIBRARY}
 )
 
 if(CMAKE_CXX_COMPILER_VERSION LESS 9.0)

+ 1 - 1
demo/VideoPlayer/widget.cpp

@@ -5,7 +5,7 @@
 #include <QFileDialog>
 
 #include "spdlog/spdlog.h"
-#include "fmtlog.h"
+// #include "fmtlog.h"
 
 // #include "VideoPlayer1.h"
 #include "VideoPlayer.h"

+ 12 - 11
demo/ftp/CMakeLists.txt

@@ -10,11 +10,11 @@ file(GLOB LOCAL_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
 
-    ${CMAKE_SOURCE_DIR}/External/common/http/*.cpp
-    ${CMAKE_SOURCE_DIR}/External/common/LightLog/*.cpp
-    ${CMAKE_SOURCE_DIR}/External/common/Logs/*.cpp
-    ${CMAKE_SOURCE_DIR}/External/common/ftp/*.cpp
-    ${CMAKE_SOURCE_DIR}/External/common/CurlFtp/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/http/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/LightLog/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/Logs/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/ftp/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/module/CurlFtp/*.cpp
     
 )
 
@@ -34,12 +34,13 @@ target_include_directories(${this_exe} PRIVATE
 
     ${CMAKE_CURRENT_SOURCE_DIR}
     ${CMAKE_SOURCE_DIR}/External/common
-    ${CMAKE_SOURCE_DIR}/External/common/http
-    ${CMAKE_SOURCE_DIR}/External/common/LightLog
-    ${CMAKE_SOURCE_DIR}/External/common/Logs
-    ${CMAKE_SOURCE_DIR}/External/common/ftp
-    ${CMAKE_SOURCE_DIR}/External/common/CurlFtp
-    ${CMAKE_SOURCE_DIR}/External/common/StdLog
+    ${CMAKE_SOURCE_DIR}/External/module
+    ${CMAKE_SOURCE_DIR}/External/module/http
+    ${CMAKE_SOURCE_DIR}/External/module/LightLog
+    ${CMAKE_SOURCE_DIR}/External/module/Logs
+    ${CMAKE_SOURCE_DIR}/External/module/ftp
+    ${CMAKE_SOURCE_DIR}/External/module/CurlFtp
+    ${CMAKE_SOURCE_DIR}/External/module/StdLog
 
     ${spdlog_INCLUDE_DIR}
     ${CURL_INCLUDE_DIR}