观察者模式(Observer Pattern)是一种行为设计模式,用于在对象之间建立一对多的依赖关系,当一个对象的状态发生变化时,它的所有依赖对象都会得到通知并自动更新。
在观察者模式中,主题和观察者之间是松耦合的,主题并不知道观察者的具体实现,只依赖于观察者接口。这种松耦合的关系使得主题和观察者能够独立变化,而不会相互影响。
#include <iostream>
#include <string>
#include <unordered_map>
#include <bits/stdc++.h>
#include <list>using namespace std;
/*
* 行为型模式:主要关注的是对象之间的通信
观察者-监听者模式 (发布-订阅模式) 设计模式:主要关注的对象是一对多的关系,
也就是说多个对象都依赖与这一个对象,当这个对象的状态发生改变时,其他对象都能够
接收到相应的通知。
*/class Observer
{
public://处理消息接口virtual void handle(int msgid) = 0;
};//第一个观察者实例
class Observer1 : public Observer
{
public:void handle(int msgid){switch (msgid){case 1:cout << "Observer1 recv 1 msg!" << endl;break;case 2:cout << "Observer1 recv 2 msg!" << endl;break;default:cout << "Observer1 recv unknow msg! " << endl;break;}}
};//第二个观察者实例
class Observer2 : public Observer
{
public:void handle(int msgid){switch (msgid){case 2:cout << "Observer2 recv 2 msg!" << endl;break;default:cout << "Observer2 recv unknow msg! " << endl;break;}}
};//第三个观察者实例
class Observer3 : public Observer
{
public:void handle(int msgid){switch (msgid){case 1:cout << "Observer3 recv 1 msg!" << endl;break;case 3:cout << "Observer3 recv 3 msg!" << endl;break;default:cout << "Observer3 recv unknow msg! " << endl;break;}}
};class Subject
{
public: //主题增加相应观察者对象void addObserver(Observer* obser, int msgid){_subMap[msgid].push_back(obser);/*auto it = _subMap.find(msgid);if (it != _subMap.end()){ it->second.push_back(obser);}else{list<Observer*> lis;lis.push_back(obser);_subMap.insert({ msgid,lis });}*/}//主题检测发生改变,通知相应的观察者对象处理事件void dispatch(int msgid) {auto it = _subMap.find(msgid);if (it != _subMap.end()){for (Observer* pObser : it->second){pObser->handle(msgid);}}}
private:unordered_map<int, list<Observer*>> _subMap;
};int main()
{Subject subject;Observer* p1 = new Observer1;Observer* p2 = new Observer2;Observer* p3 = new Observer3;subject.addObserver(p1, 1);subject.addObserver(p1, 2);subject.addObserver(p2, 2);subject.addObserver(p3, 1);subject.addObserver(p3, 3);int msgid = 0;for (;;){cout << " 输入消息id: ";cin >> msgid;if (msgid == -1) break;subject.dispatch(msgid);}return 0;
}
输入消息id: 2
Observer1 recv 2 msg!
Observer2 recv 2 msg!输入消息id: 1
Observer1 recv 1 msg!
Observer3 recv 1 msg!输入消息id: 3
Observer3 recv 3 msg!
主要解决的是对象与对象之间的依赖关系,多个对象依赖于一个对象,该对象主要负责检测我们消息发没发生状态改变,有改变了就要通知相应的观察者。
这里涉及多对象内存开辟问题,需要使用智能指针去优化:
#include<iostream>
#include<unordered_map>
#include<list>
#include<bits/stdc++.h>
#include<memory>class Observer
{
public:virtual void handle(int msgid) = 0; //纯虚函数没有具体实现
};class Observer1 : public Observer
{
public:void handle(int msgid){switch (msgid){case 1:std::cout << "Observer 1 recv 1 msg!" << std::endl;break;case 2:std::cout << "Observer 1 recv 2 msg!" << std::endl;break;default:std::cout << "Observer 1 recv unknow msg!" << std::endl;break;}}
};class Observer2 : public Observer
{
public:void handle(int msgid){switch (msgid){case 2:std::cout << "Observer 2 recv 2 msg!" << std::endl;break;default:std::cout << "Observer 2 recv unknow msg! " << std::endl;break;}}
};class Observer3 : public Observer
{
public:void handle(int msgid){switch (msgid){case 1:std::cout << "Obersever 3 recv 1 msg!" << std::endl;break;case 3:std::cout << "Obersever 3 recv 3 msg!" << std::endl;break;default:std::cout << "Observer 3 recv unknow msg! " << std::endl;break;}}
};class Subject
{
public:void addObserver(std::weak_ptr<Observer> obser, int msgid){_subMap[msgid].push_back(obser);}void dispatch(int msgid){auto it = _subMap.find(msgid);if (it != _subMap.end()){for (auto it1 = it->second.begin(); it1 != it->second.end(); ++it1){ // 与强指针绑定的弱指针,通过lock函数提升为强指针,可以避免循环引用问题,同时判断所绑定的对象是否被销毁std::shared_ptr<Observer>tmp = it1->lock();if (tmp) {tmp->handle(msgid);}}}}
private:std::unordered_map<int, std::list<std::weak_ptr<Observer>> > _subMap;
};int main()
{Subject subject;std::shared_ptr<Observer>p1(new Observer1);std::shared_ptr<Observer>p2(new Observer2);std::shared_ptr<Observer>p3(new Observer3);// shared_ptr赋值给weak_ptr,绑定subject.addObserver(p1, 1);subject.addObserver(p1, 2);subject.addObserver(p2, 2);subject.addObserver(p3, 1);subject.addObserver(p3, 3);int msgid = 0;for (;;){std::cout << " 输入消息id: ";std::cin >> msgid;if (msgid == -1) break;subject.dispatch(msgid);}return 0;
}
重点就是这里,一开始用弱指针去绑定一些对象subject.addObserver(p1, 1);
,通过弱指针提升为强指针,来判断所绑定的对象是否被销毁std::shared_ptr<Observer>tmp = it1->lock();
(muduo项目中,channel与TcpConnection对象绑定):
std::shared_ptr<Observer>tmp = it1->lock();
if (tmp) {tmp->handle(msgid);
}