设计模式之观察者模式
- 代码示例
#ifndef OBSERVER_H
#define OBSERVER_H#include <map>class Observer
{
public:Observer();virtual void update(std::map<int, double>) = 0;
};
#endif // OBSERVER_H#include "observer.h"Observer::Observer()
{}
#ifndef KAFKAPUSHER_H
#define KAFKAPUSHER_H
#include "observer.h"
#include <map>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__);
}
#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__);
}
#ifndef SUBSCRIBER_H
#define SUBSCRIBER_H#include <list>
#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);
}
#ifndef WORKER_H
#define WORKER_H#include <map>
#include <list>
#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 <cstdlib>
#include <ctime>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();
}
#include <iostream>
#include <unistd.h>
#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;
}