• EventSystem(事件派发-观察者模式)


    EventSystem(事件派发-观察者模式

    观察者模式学习链接:https://blog.csdn.net/qq135595696/article/details/121481732

      可自定义主题(结构体),对应观察者事件可以通过订阅主题收到发布者消息,同时该主题也作为消息结构传入发布者事件中。

    #include 
    #include 
    #include 
    
    namespace {
        //主题(同时也是函数的参数)
        struct Test1 {
            int a;
            int b;
        };
    
    
        struct Test2 {
            int c;
            int d;
        };
    }
    
    
    class EventSystem {
    public:
        static EventSystem* Ins() {
            static EventSystem ins;
            return &ins;
        }
    
        template<typename T>
        bool PushEvent(void* pFunc) {
            m_unMaps[typeid(T).name()].push_back(pFunc);
            return true;
        }
    
        template<typename T>
        bool CallEvent(T* data) {
            typedef void(*FuncType)(T const*);
    
            auto iter = m_unMaps.find(typeid(T).name());
            for (auto item = iter->second.begin(); item != iter->second.end(); item++) {
                FuncType event = (FuncType)(*item);
                event(data);
            }
    
            return true;
        }
    
    private:
        EventSystem()  {}
        ~EventSystem() {}
    
    private:
        std::unordered_map<const char*, std::vector<void*>> m_unMaps;
    };
    
    
    //订阅Test1,即此处为观察者观察主题
    void Test(Test1* const test) {
        std::cout << "Test:" << test->a << " " << test->b << std::endl;
    }
    
    void TestP(Test1* const test) {
        std::cout << "TestP:" << test->a << " " << test->b << std::endl;
    }
    
    
    //订阅Test2,即此处为观察者观察主题
    void TestP1(Test2* const test) {
        std::cout << "TestP:" << test->c << " " << test->d << std::endl;
    }
    
    int main() {
        //订阅Test1消息
        EventSystem::Ins()->PushEvent<Test1>(Test);
        EventSystem::Ins()->PushEvent<Test1>(TestP);
    
        //发布者发布消息,订阅Test1的观察者都能收到消息
        Test1 test{ 1,2 };
        EventSystem::Ins()->CallEvent<Test1>(&test);
    
    
        //订阅Test2消息
        EventSystem::Ins()->PushEvent<Test2>(TestP);
    
        //发布者发布消息,订阅Test2的观察者都能收到消息
        Test2 test1{ 3333,5555 };
        EventSystem::Ins()->CallEvent<Test2>(&test1);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    效果图

  • 相关阅读:
    力扣(LeetCode)565. 数组嵌套(C++)
    08c++呵呵老师【给子弹添加爆炸效果】
    买电脑常识——电脑性能
    xcode iOS 在app文件中开启访问 Document Directory
    每日一练 | 华为认证真题练习Day115
    [附源码]计算机毕业设计基于Springboot影院管理系统
    Paramenter-Efficient Transfer Learning for NLP
    超级兔子人
    区块链技术的飞跃: 2023年的数字革命
    数据库原理与应用学习要点
  • 原文地址:https://blog.csdn.net/qq135595696/article/details/127838401