QtExamples/Studio/DesignPatterns/Observer.h

107 lines
3.2 KiB
C
Raw Normal View History

2022-12-24 01:17:47 +08:00
/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: Observer Pattern
* Reference: Subject Observer ABC
*
* See: https://www.cnblogs.com/suzhou/p/dp16obsvr.html
******************************************************************/
#pragma once
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Observer { // 观察者
public:
virtual void update(string msg) = 0;
virtual void doSomeThing() = 0;
protected:
string m_msg;
};
class Subject {
public:
Subject() = default;
virtual ~Subject() = default;
virtual void addObsvr(Observer* obj) = 0;
virtual void removeObsvr(Observer* obj) = 0;
virtual void notify(string msg) = 0;
};
//---------------------------------------------------------
class ConcreteSubject: public Subject {
public:
ConcreteSubject() {};
virtual ~ConcreteSubject() { m_obsvrs.clear(); }
virtual void addObsvr(Observer* obj) override {
m_obsvrs.push_back(obj);
}
virtual void removeObsvr(Observer* obj) override {
m_obsvrs.remove(obj);
}
virtual void notify(string msg) override {
for (const auto& it : m_obsvrs)
it->update(msg);
for (const auto& it : m_obsvrs)
it->doSomeThing();
cout << endl;
}
private:
list<Observer*> m_obsvrs;
};
class AObserver : public Observer {
public:
void update(string msg) override { m_msg = msg; }
virtual void doSomeThing() override { cout << "AObserver: 对 " << m_msg << " 进行 A 的操作" << endl; }
};
class BObserver : public Observer {
public:
void update(string msg) override { m_msg = msg; }
virtual void doSomeThing() override { cout << "BObserver: 对 " << m_msg << " 进行 B 的操作" << endl; }
};
class CObserver : public Observer {
public:
void update(string msg) override { m_msg = msg; }
virtual void doSomeThing() override { cout << "CObserver: 对 " << m_msg << " 进行 C 的操作" << endl; }
};
int main()
{
AObserver* a = new AObserver();
BObserver* b = new BObserver();
CObserver* c = new CObserver();
ConcreteSubject* concreSubject = new ConcreteSubject();
concreSubject->addObsvr(a);
concreSubject->addObsvr(b);
concreSubject->addObsvr(c);
concreSubject->notify("messagebox 1");
concreSubject->removeObsvr(c);
concreSubject->notify("messagebox 2");
//a->doSomeThing();
//b->doSomeThing();
//c->doSomeThing();
return 0;
}
/*****************************打印结果*******************************
AObserver: messagebox 1 A
BObserver: messagebox 1 B
CObserver: messagebox 1 C
AObserver: messagebox 2 A
BObserver: messagebox 2 B
******************************************************************/