• Design patterns--观察者模式


    设计模式观察者模式

    • 代码示例
    #ifndef OBSERVER_H
    #define OBSERVER_H
    
    #include 
    
    class Observer
    {
    public:
        Observer();
        virtual void update(std::map<int, double>) = 0;
    };
    #endif // OBSERVER_H
    
    
    #include "observer.h"
    
    Observer::Observer()
    {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    #ifndef KAFKAPUSHER_H
    #define KAFKAPUSHER_H
    #include "observer.h"
    #include 
    
    class KafkaPusher : public Observer
    {
    public:
        KafkaPusher();
    
        void update(std::map<int, double> msgPoints) override;
        void display();
    
    private:
        std::map<int, double> m_sailing;
    };
    
    #endif // KAFKAPUSHER_H
    
    #include "kafkapusher.h"
    
    KafkaPusher::KafkaPusher()
    {
    
    }
    
    void KafkaPusher::update(std::map<int, double> msgPoints)
    {
        for(auto itr = msgPoints.begin(); itr != msgPoints.end(); ++itr)
        {
            if(itr->first % 2 != 0){
                m_sailing[itr->first] = itr->second;
            }
        }
    }
    
    void KafkaPusher::display()
    {
        for(auto itr = m_sailing.begin(); itr != m_sailing.end(); ++itr)
        {
            printf("%d:%.3f, ", itr->first, itr->second);
        }
        printf("\n[%s:%d]\n", __FILE__, __LINE__);
    }
    
    • 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
    #ifndef RABBITMQPUSHER_H
    #define RABBITMQPUSHER_H
    #include "observer.h"
    
    class RabbitMQPusher : public Observer
    {
    public:
        RabbitMQPusher();
    
        void update(std::map<int, double> msgPoints) override;
        void display();
    
    private:
        std::map<int, double> m_engine;
    };
    
    #endif // RABBITMQPUSHER_H
    
    #include "rabbitmqpusher.h"
    
    RabbitMQPusher::RabbitMQPusher()
    {
    
    }
    
    void RabbitMQPusher::update(std::map<int, double> msgPoints)
    {
        for(auto itr = msgPoints.begin(); itr != msgPoints.end(); ++itr)
        {
            if(itr->first % 2 == 0){
                m_engine[itr->first] = itr->second;
            }
        }
    }
    
    void RabbitMQPusher::display()
    {
        for(auto itr = m_engine.begin(); itr != m_engine.end(); ++itr)
        {
            printf("%d:%.3f, ", itr->first, itr->second);
        }
        printf("\n[%s:%d]\n", __FILE__, __LINE__);
    }
    
    • 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

    #ifndef SUBSCRIBER_H
    #define SUBSCRIBER_H
    
    #include 
    #include "observer.h"
    
    class Subscriber
    {
    public:
        Subscriber();
        void registerObserver(Observer* obItem);
        void removeObserver(Observer* obItem);
    
        virtual void notify() = 0;
    
    protected:
        std::list<Observer*> m_listObserver;
    };
    
    #endif // SUBSCRIBER_H
    
    #include "subscriber.h"
    
    Subscriber::Subscriber()
    {
    
    }
    
    void Subscriber::registerObserver(Observer* obItem)
    {
        m_listObserver.push_back(obItem);
    }
    
    void Subscriber::removeObserver(Observer* obItem)
    {
        m_listObserver.remove(obItem);
    }
    
    • 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
    #ifndef WORKER_H
    #define WORKER_H
    
    #include 
    #include 
    #include "subscriber.h"
    
    class Worker : public Subscriber
    {
    public:
        Worker();
        void notify() override;
    
        void alterOperationPoints(std::map<int, double> op);
    
    private:
        std::map<int, double> m_OperatePoints;
    };
    
    #endif // WORKER_H
    
    #include "worker.h"
    #include 
    #include 
    
    Worker::Worker()
    {
    
    }
    
    void Worker::notify()
    {
        std::list<Observer*>::iterator itr;
        for(itr = m_listObserver.begin(); itr != m_listObserver.end(); ++itr){
            (*itr)->update(m_OperatePoints);
        }
    }
    
    void Worker::alterOperationPoints(std::map<int, double> op)
    {
        srand(time(0));
        for(int i = 51; i <= 60; i++)
        {
            int random_number = rand() % 100 + 1;
            printf("%d ", random_number);
            m_OperatePoints[i] = random_number;
        }
        printf("\n");
        notify();
    }
    
    • 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

    #include 
    #include 
    #include "worker.h"
    #include "kafkapusher.h"
    #include "rabbitmqpusher.h"
    
    using namespace std;
    
    int main()
    {
        KafkaPusher kPush;
        RabbitMQPusher rPush;
    
        Worker worker;
        worker.registerObserver(&kPush);
        worker.registerObserver(&rPush);
    
        std::map<int, double> bigData;
        while (true) {
            worker.alterOperationPoints(bigData);
    
            kPush.display();
            rPush.display();
    
            sleep(2);
        }
    
        cout << "==Over==" << endl;
        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
  • 相关阅读:
    web前端三大主流框架
    (整理)蓝屏代码对应原因_蓝屏问题解决方法
    git fatal: detected dubious ownership in repository 解决方法
    盘点≠走过场,哪些功能可以进行高效库存盘点?
    【Pytorch】Yolov5中CPU转GPU过程报错完善留档归纳
    Apache InLong 1.2 单机部署问题记录
    JavaWeb项目案例(一)
    [golang]channel源码
    电力系统数字化升级改造之配电室无人值守
    安装Mysql报错:Could not create or access the registry key needed for the...
  • 原文地址:https://blog.csdn.net/Love_XiaoQinEr/article/details/133718964