Forráskód Böngészése

V1.1.5
1、设计模式新增了中介者模式

Apple 2 napja
szülő
commit
b19f1091fd

+ 2 - 0
demo/DesignerPattern/CMakeLists.txt

@@ -17,6 +17,7 @@ file(GLOB LOCAL_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/FlyWeight/*.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/Combination/*.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/CommandModel/*.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/MediatorPattern/*.cpp
 )
 
 
@@ -44,6 +45,7 @@ target_include_directories(${this_exe} PRIVATE
     # ${CMAKE_CURRENT_SOURCE_DIR}/Iterator/RingQueue
     ${CMAKE_CURRENT_SOURCE_DIR}/Combination
     ${CMAKE_CURRENT_SOURCE_DIR}/CommandModel
+    ${CMAKE_CURRENT_SOURCE_DIR}/MediatorPattern
 )
 
 target_link_libraries(${this_exe} PRIVATE

+ 119 - 0
demo/DesignerPattern/MediatorPattern/MediatorPattern.cpp

@@ -0,0 +1,119 @@
+
+#include "MediatorPattern.h"
+
+
+using namespace MediatorPattern;
+
+
+void MediatorPattern::Alabasta::Declare(std::string msg, std::string country)
+{
+    m_mediator->Declare(msg, this, country);
+}
+void MediatorPattern::Alabasta::setMessage(std::string msg)
+{
+    SPDLOG_INFO("阿拉巴斯坦得到的消息: {}", msg);
+}
+std::string MediatorPattern::Alabasta::getName()
+{
+    return "阿拉巴斯坦";
+}
+
+
+void MediatorPattern::Dressrosa::Declare(std::string msg, std::string country)
+{
+    m_mediator->Declare(msg, this, country);
+}
+void MediatorPattern::Dressrosa::setMessage(std::string msg)
+{
+    SPDLOG_INFO("德雷斯罗萨得到的消息: {}", msg);
+}
+std::string MediatorPattern::Dressrosa::getName()
+{
+    return "德雷斯罗萨";
+}
+
+void MediatorPattern::Lulusia::Declare(std::string msg, std::string country)
+{
+    m_mediator->Declare(msg, this, country);
+}
+void MediatorPattern::Lulusia::setMessage(std::string msg)
+{
+    SPDLOG_INFO("露露西亚得到的消息: {}", msg);
+}
+std::string MediatorPattern::Lulusia::getName()
+{
+    return "露露西亚";
+}
+
+void MediatorPattern::Kamabaka::Declare(std::string msg, std::string country)
+{
+    m_mediator->Declare(msg, this, country);
+}
+void MediatorPattern::Kamabaka::setMessage(std::string msg)
+{
+    SPDLOG_INFO("卡玛巴卡得到的消息: {}", msg);
+}
+std::string MediatorPattern::Kamabaka::getName()
+{
+    return "卡玛巴卡";
+}
+
+
+void MediatorPattern::AbstractMediator::addCountry(AbstractCountry* country)
+{
+    m_mapCountry[country->getName()] = country;
+}
+
+
+void MediatorPattern::WorldGovernment::Declare(std::string message, AbstractCountry* country, std::string name)
+{
+    if(m_mapCountry.find(name) != m_mapCountry.end())
+    {
+        SPDLOG_DEBUG("[WorldGovernment] <{}>向<{}>宣布消息: {}", country->getName(), name, message);
+        m_mapCountry[name]->setMessage(message);
+    } else
+    {
+        SPDLOG_WARN("[WorldGovernment] 未找到国家: {}", name);
+    }
+}
+
+
+void MediatorPattern::GeMingLeague::Declare(std::string message, AbstractCountry* country, std::string name)
+{
+    std::string msg = fmt::format("[GeMingLeague] 消息通报, 来自[{}]: {}", country->getName(), message);
+    for(auto& pair : m_mapCountry)
+    {
+        if(pair.second->getName() != country->getName())
+        {
+            pair.second->setMessage(msg);
+        }
+    }
+}
+
+
+
+void MediatorPattern::testMediatorPattern()
+{
+    /* 世界政府 */
+    WorldGovernment* worldGov = new WorldGovernment();
+    AbstractCountry* alabasta = new Alabasta(worldGov);
+    AbstractCountry* dressrosa = new Dressrosa(worldGov);
+    worldGov->addCountry(alabasta);
+    worldGov->addCountry(dressrosa);
+    // 世界政府成员发声
+    alabasta->Declare("德雷斯罗萨倒卖军火, 搞得我国连年打仗, 必须给个说法!!!", dressrosa->getName());
+    dressrosa->Declare("天龙人都和我多弗朗明哥做生意, 你算老几, 呸!!!", alabasta->getName());
+
+    SPDLOG_INFO("--------------------------------------------------------------------------");
+    /* 革命军 */
+    GeMingLeague* gemingLeague = new GeMingLeague();
+    AbstractCountry* lulusia = new Lulusia(gemingLeague);
+    AbstractCountry* kamabaka = new Kamabaka(gemingLeague);
+    gemingLeague->addCountry(lulusia);
+    gemingLeague->addCountry(kamabaka);
+    lulusia->Declare("我草, 我的国家被伊姆毁灭了!!!", lulusia->getName());
+
+    SPDLOG_INFO("测试完成!");
+}
+
+

+ 138 - 0
demo/DesignerPattern/MediatorPattern/MediatorPattern.h

@@ -0,0 +1,138 @@
+#ifndef __MEDIATORPATTERN_H__
+#define __MEDIATORPATTERN_H__
+
+
+/*
+    中介者模式(Mediator Pattern)是一种行为设计模式,
+    它通过引入一个中介者对象来封装一组对象之间的交互,
+    使得这些对象不需要直接引用彼此,从而降低它们之间的耦合度。
+    中介者模式的主要目的是简化对象之间的通信,
+    使得系统更易于维护和扩展。
+    主要角色:
+    1. 中介者(Mediator):定义一个接口,用于与各个同事对象进行通信。
+    2. 具体中介者(Concrete Mediator):实现中介者接口,协调各个同事对象之间的交互。
+    3. 同事对象(Colleague):与中介者通信的对象,不直接引用其他同事对象。
+    优点:
+    - 降低对象之间的耦合度,使得系统更易于维护和扩展。
+    - 集中控制对象之间的交互,简化了通信逻辑。
+    缺点:
+    - 中介者对象可能变得过于复杂,难以维护。
+    使用场景:
+    - 对象之间存在复杂的交互关系,需要集中管理这些交互。
+    - 希望降低对象之间的耦合度,提高系统的可维护性。
+
+    如: MQTT 协议中的消息代理充当中介者,协调发布者和订阅者之间的通信。
+*/
+
+#include "spdlog/spdlog.h"
+#include <string>
+#include <map>
+
+namespace MediatorPattern
+{
+
+class AbstractMediator;
+
+/* ==================================================================
+ * 抽象对象
+ * ================================================================== */
+
+class AbstractCountry
+{
+public:
+    AbstractCountry(AbstractMediator* mediator) : m_mediator(mediator) {}
+    virtual ~AbstractCountry() {};
+    virtual void Declare(std::string message, std::string) = 0;
+    /* 接收消息 */
+    virtual void setMessage(std::string message) = 0;
+    virtual std::string getName() = 0;
+
+protected:
+    AbstractMediator* m_mediator;
+};
+
+
+/* 具体国家 */
+// 阿拉巴斯坦
+class Alabasta : public AbstractCountry
+{
+public:
+    using AbstractCountry::AbstractCountry;
+    void Declare(std::string msg, std::string country) override;
+    void setMessage(std::string msg) override;
+    std::string getName() override;
+};
+
+// 德雷斯罗萨
+class Dressrosa : public AbstractCountry
+{
+public:
+    using AbstractCountry::AbstractCountry;
+    void Declare(std::string msg, std::string country) override;
+    void setMessage(std::string msg) override;
+    std::string getName() override;
+};
+
+// 露露西亚王国
+class Lulusia : public AbstractCountry
+{
+public:
+    using AbstractCountry::AbstractCountry;
+    void Declare(std::string msg, std::string country) override;
+    void setMessage(std::string msg) override;
+    std::string getName() override;
+};
+
+// 卡玛巴卡王国
+class Kamabaka : public AbstractCountry
+{
+public:
+    using AbstractCountry::AbstractCountry;
+    void Declare(std::string msg, std::string country) override;
+    void setMessage(std::string msg) override;
+    std::string getName() override;
+};
+
+
+
+
+/* ==================================================================
+ * 中介
+ * ================================================================== */
+
+/* 抽象中介类 */
+class AbstractMediator
+{
+public:
+    virtual ~AbstractMediator() {};
+    virtual void addCountry(AbstractCountry* country);
+    virtual void Declare(std::string message, AbstractCountry* country, std::string name = std::string()) = 0;
+protected:
+    std::map<std::string, AbstractCountry*> m_mapCountry;
+};
+
+/* 世界政府,指定某个国家发送消息 */
+class WorldGovernment : public AbstractMediator
+{
+public:
+    void Declare(std::string message, AbstractCountry* country, std::string name = std::string()) override;
+};
+
+/* 革命军,全部通报消息 */
+class GeMingLeague : public AbstractMediator
+{
+public:
+    void Declare(std::string message, AbstractCountry* country, std::string name = std::string()) override;
+};
+
+
+
+
+void testMediatorPattern();
+}
+
+
+
+
+#endif /* __MEDIATORPATTERN_H__ */
+

+ 4 - 1
demo/DesignerPattern/main.cpp

@@ -11,6 +11,7 @@
 #include "Iterator/IteratorDemo.h"
 #include "FlyWeight/FlyWeifht.h"
 #include "CommandModel/CommandModel.h"
+#include "MediatorPattern/MediatorPattern.h"
 
 
 int main(int argc, char *argv[])
@@ -50,11 +51,13 @@ int main(int argc, char *argv[])
     // delete translator;
     // delete french;
 
-    testRingQueueManualMutex();
+    // testRingQueueManualMutex();
 
     // testFlyWeight();
 
     // CommandModel::useRemoteControl();
 
+    MediatorPattern::testMediatorPattern();
+
     return a.exec();
 }