c++设计模式之观察者模式
视频地址:
https://www.ixigua.com/6804249883344634380?id=6803643021414892046
文档整理地址:
观察者模式样例类图:
样例代码:
1 #include<iostream> 2 #include <algorithm> 3 #include <string> 4 #include <list> 5 #include <memory> 6 using namespace std; 7 8 enum class Position 9 { 10 CPP, 11 JAVA, 12 PHP, 13 C_Sharp, 14 Python 15 }; 16 17 class Engineer 18 { 19 public: 20 Engineer(const string &name) :name_(name) {}; 21 virtual void update(Position position) = 0; 22 protected: 23 string name_; 24 25 }; 26 27 class CppEngineer:public Engineer 28 { 29 public: 30 CppEngineer(const string &name) :Engineer(name) {}; 31 virtual void update(Position position)override 32 { 33 if (position == Position::CPP) 34 { 35 cout << name_ << " 投递简历!" << endl; 36 } 37 } 38 }; 39 40 class PHPEngineer :public Engineer 41 { 42 public: 43 PHPEngineer(const string&name) :Engineer(name) {}; 44 virtual void update(Position position)override 45 { 46 if (position == Position::PHP) 47 { 48 cout << name_ << " 投递简历!" << endl; 49 } 50 } 51 }; 52 53 class JAVAEngineer :public Engineer 54 { 55 public: 56 JAVAEngineer(const string&name) :Engineer(name) {}; 57 virtual void update(Position position)override 58 { 59 if (position == Position::JAVA) 60 { 61 cout << name_ << " 投递简历!" << endl; 62 } 63 } 64 }; 65 66 67 68 class Alibaba 69 { 70 public: 71 void Recruit(Position position) //招聘某种职位 72 { 73 Notify(position); 74 } 75 void AddEngineer(shared_ptr<Engineer> engineer) 76 { 77 engineer_list.push_back(engineer); 78 } 79 private: 80 list<shared_ptr<Engineer>> engineer_list; 81 void Notify(Position position) //通知 82 { 83 for (const auto &engineer : engineer_list) 84 { 85 engineer->update(position); 86 } 87 } 88 }; 89 90 int main() 91 { 92 Alibaba ali; 93 ali.AddEngineer(make_shared<CppEngineer>("C++程序员")); 94 ali.AddEngineer(make_shared<JAVAEngineer>("Java程序员")); 95 ali.AddEngineer(make_shared<PHPEngineer>("PHP程序员")); 96 97 ali.Recruit(Position::CPP); 98 ali.Recruit(Position::Python); 99 ali.Recruit(Position::C_Sharp); 100 ali.Recruit(Position::PHP); 101 }
参考二:
观察者模式样例类图:
样例代码:
1 #include <iostream> 2 #include <list> 3 using namespace std; 4 5 /*抽象观察者*/ 6 class Observer 7 { 8 public: 9 virtual void Update(int) = 0; 10 }; 11 12 /*抽象目标*/ 13 class Subject 14 { 15 public: 16 virtual void Attach(Observer *) = 0; //附加观察者 17 virtual void Detach(Observer *) = 0; //移除观察者 18 virtual void Notify() = 0; //通知观察者 19 }; 20 21 /*具体观察者*/ 22 class ConcreteObserver : public Observer 23 { 24 public: 25 ConcreteObserver(Subject *pSubject) : m_pSubject(pSubject){} 26 27 void Update(int value) 28 { 29 cout << "ConcreteObserver get the update. New State:" << value << endl; 30 } 31 32 private: 33 Subject *m_pSubject; 34 }; 35 /*具体观察者2*/ 36 class ConcreteObserver2 : public Observer 37 { 38 public: 39 ConcreteObserver2(Subject *pSubject) : m_pSubject(pSubject){} 40 41 void Update(int value) 42 { 43 cout << "ConcreteObserver2 get the update. New State:" << value << endl; 44 } 45 46 private: 47 Subject *m_pSubject; 48 }; 49 50 /*具体目标*/ 51 class ConcreteSubject : public Subject 52 { 53 public: 54 void Attach(Observer *pObserver); 55 void Detach(Observer *pObserver); 56 void Notify(); 57 58 void SetState(int state) 59 { 60 m_iState = state; 61 } 62 63 private: 64 std::list<Observer *> m_ObserverList;//观察者列表 65 int m_iState; 66 }; 67 68 //附加观察者 69 void ConcreteSubject::Attach(Observer *pObserver) 70 { 71 m_ObserverList.push_back(pObserver); 72 } 73 74 //移除观察者 75 void ConcreteSubject::Detach(Observer *pObserver) 76 { 77 m_ObserverList.remove(pObserver); 78 } 79 80 //通知观察者 81 void ConcreteSubject::Notify() 82 { 83 std::list<Observer *>::iterator it = m_ObserverList.begin(); 84 while (it != m_ObserverList.end()) 85 { 86 (*it)->Update(m_iState); 87 ++it; 88 } 89 } 90 91 int main() 92 { 93 // Create Subject 目标实例化 94 ConcreteSubject *pSubject = new ConcreteSubject(); 95 96 // Create Observer 创建观察者 97 Observer *pObserver = new ConcreteObserver(pSubject); 98 Observer *pObserver2 = new ConcreteObserver2(pSubject); 99 100 // Change the state 101 pSubject->SetState(2); 102 103 // Register the observer 104 pSubject->Attach(pObserver); 105 pSubject->Attach(pObserver2); 106 107 pSubject->Notify(); 108 109 // Unregister the observer 110 pSubject->Detach(pObserver); 111 112 pSubject->SetState(3); 113 pSubject->Notify(); 114 115 delete pObserver; 116 delete pObserver2; 117 delete pSubject; 118 }
观察者模式:
流程:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧