|
|
@@ -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("测试完成!");
|
|
|
+}
|
|
|
+
|
|
|
+
|