实例:
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
// 观察者基类,提供供发布者调用的通用接口Update()函数;
class IObserver {
public:
virtual ~IObserver() {};
virtual void Update(const std::string &message) = 0;
};
// 发布者基类,提供通用的添加观察者(Attach)、删除观察者(Dttach)、通知观察者(Notify)函数。
class IPublisher {
public:
virtual ~IPublisher(){};
virtual void Attach(IObserver *observer) = 0;
virtual void Detach(IObserver *observer) = 0;
virtual void Notify() = 0;
};
/**
* The Publisher owns some important state and notifies observers when the state
* changes.
*/
// 发布者类:实现通用函数,观察者观察的状态函数(该函数中,调用Notify函数)。
// 发布者类需要提供存储观察者(订阅者)的信息,用于通知
class Publisher : public IPublisher {
public:
virtual ~Publisher()
{
std::cout << "Goodbye, I was the Publisher" << std::endl;
}
/**
* The subscription management methods.
*/
void Attach(IObserver *observer) override
{
list_observers.push_back(observer);
}
void Detach(IObserver *observer) override
{
list_observers.remove(observer);
}
void Notify() override
{
for (auto iter : list_observers) {
iter->Update(message);
}
}
void CreateMessage(std::string message = "empty")
{
this->message = message;
Notify();
}
void SomethingImportant() {
this->message = "changed";
std::cout << "I have something important to do, so notify you all" << std::endl;
Notify();
}
private:
std::list<IObserver *> list_observers;
std::string message;
};
// 观察者类:实现发布者调用的接口Update()函数,及状态变更的响应函数MyCorrespondAct()。 // 同时给发布者发消息用于发布者添加/删除订阅者(可以第三方进行管理,也可以传入发布者指针,自己管理)
// 订阅:1、观察者直接调用发布者的函数进行增删(需要拥有发布者指针);
// 2、第三方,如主机(main)调用发布者函数进行增删;
// 3、发布者设计为单例或多件模式,从而可以使用时获取发布者对象来调用函数
class Observer : public IObserver {
public:
Observer(Publisher &publisher) : publisher_(publisher) {
this->publisher_.Attach(this);
std::cout << "I am the Observer " << ++Observer::static_number_ << std::endl;
this->number_ = Observer::static_number_;
}
virtual ~Observer() {
std::cout << "Goodbye, I was the Observer " << number_ << std::endl;
}
void Update(const std::string &message) override
{
message_from_publisher_ = message;
MyCorrespondAct();
}
void MyCorrespondAct() {
std::cout << "Observer " << number_ << " I received a new message " << message_from_publisher_ << std::endl;
}
void RemoveMeFromTheList() {
publisher_.Detach(this);
std::cout << "Observer " << number_ << "removed from the list" << std::endl;
}
private:
std::string message_from_publisher_;
Publisher &publisher_;
static int static_number_;
int number_;
};
int Observer::static_number_ = 0;
void ClientCode()
{
Publisher *publisher = new Publisher;
Observer *observer1 = new Observer(*Publisher);
Observer *observer2 = new Observer(*Publisher);
Observer *observer3 = new Observer(*Publisher);
Observer *observer4;
Observer *observer5;
publisher->CreateMessage("Hello World!");
observer3->RemoveMeFromTheList();
publisher->CreateMessage("Hello World!");
observer4 = new Observer(*publisher);
observer2->RemoveMeFromTheList();
observer5 = new Observer(*publisher);
delete observer5;
delete observer4;
delete observer3;
delete observer2;
delete observer1;
delete publisher;
}
int main()
{
ClientCode();
return 0;
}
学习链接:观察者模式