观察者模式是一种行为型设计模式,它允许对象自动向其依赖的观察者对象通知其状态的改变。
在观察者模式中,有两个主要角色:主题和观察者。主题维护一个观察者列表,当主题的状态发生改变时,它会通知所有的观察者。观察者则需要实现一个更新方法,以便在接收到主题的通知时执行相应的操作。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<memory>
using namespace std;
class Observer;
//主题接口
class Subject {
public:
virtual void registerObserver(shared_ptr<Observer> observer)=0;
virtual void removeObserver(shared_ptr<Observer> observer)=0;
virtual void notifyObservers() = 0;
};
//抽象观察者类
class Observer {
public:
virtual void update(int value) = 0;
};
//具体主题类
class MySubject :public Subject {
public:
void registerObserver(shared_ptr<Observer> observer)override;
void removeObserver(shared_ptr<Observer> observer) override;
void notifyObservers() override;
void setValue(int value);
private:
int value{ 0 };
std::vector<shared_ptr<Observer>>observers;
};
void MySubject::registerObserver(shared_ptr<Observer> observer) {
.push_back(observer);
observers}
void MySubject::removeObserver(shared_ptr<Observer> observer) {
auto iter = std::find(observers.begin(), observers.end(), observer);
if (iter != observers.end()) {
.erase(iter);
observers}
}
void MySubject::notifyObservers() {
for (auto iter = observers.begin(); iter != observers.end();++iter) {
(* iter)->update(value);
}
}
void MySubject::setValue(int value) {
this->value = value;
();
notifyObservers}
//具体观察者类
class MyObserver :public Observer {
public:
(const std::string& name);
MyObservervoid update(int value) override;
private:
std::string name;
};
::MyObserver(const std::string& name):name(name)
MyObserver{
}
void MyObserver::update(int value) {
<< "Observer " << name << " received update with value " << value << endl;
cout }
int main(int argc, char** argv) {
<MySubject>subject = make_shared<MySubject>();
shared_ptr//观察者对象
<Observer> observer1{ new MyObserver("observer1")};
shared_ptr<Observer> observer2{ new MyObserver("observer2") };
shared_ptr->registerObserver(observer1);
subject->registerObserver(observer2);
subject//改变主题状态
->setValue(12);
subject//输出:Observer observer1 received update with value 12
// Observer observer2 received update with value 12
//observer2 从主题中移除
->removeObserver(observer2);
subject->setValue(1);
subject//输出:Observer observer1 received update with value 1
return 0;
}