日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++设计模式--观察者模式(Observer)

發布時間:2025/1/21 c/c++ 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++设计模式--观察者模式(Observer) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述

觀察者模式,定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時,所有依賴于它的對象都得到通知并被自動更新。

適用場景

以下任一情況下可以使用觀察者模式:

  • 當一個抽象模型有兩方面,其中一個方面依賴于另一個方面。將這二者封裝在獨立的對象中以使它們可以各自獨立地改變和復用。
  • 當一個對象的改變需要同時改變其它對象,而不知道具體有多少對象有待改變。
  • 當一個對象必須通知其它對象,而它又不能假定其它對象是誰。換言之,你不希望這些對象是緊密耦合的。

UML 類圖

  • Subject(抽象目標)
    – 目標知道它的觀察者??梢杂腥我舛鄠€觀察者觀察同一個目標
    – 提供住處和刪除觀察者對象的接口
  • Observer(觀察者)
    – 為那些在目標發生改變時需獲得通知的對象定義一個更新接口‘
  • ConcreteSubject(具體目標)
    – 將有關狀態存入各個 ConcreteObserver 對象
    – 當它的狀態發生改變時,向它的各個觀察者發出通知
  • ConcreteObserver(具體觀察者)
    – 維護一個指向 ConcreteSubject 對象的引用
    – 存儲有關狀態,這些狀態應與目標的狀態保持一致
    – 實現Observer的更新接口以使自身狀態與目標的狀態保持一致

示例

假定一個學校,若要發布向所有班級發布重要通知,需要由每個班主任一一的到自己班級上進行通知,那么這里的班主任就相當于目標主題,而每個班級的學生就是觀察者。

來看下代碼:
isubject.h

#include <string> #include <list> class IObserver;using namespace std; //抽象目標 class ISubject { public:virtual ~ISubject();virtual void Attach(IObserver * o);virtual void Detach(IObserver * o);virtual void Notify(string msg); private:list<IObserver *> m_observers; };//具體目標 class ConcreteSubject : public ISubject { public:~ConcreteSubject();void setMessage(string msg);string getMssage();private:string m_msg; };

isubject.cpp

#include "isubject.h" #include "iobserver.h"ISubject::~ISubject() {}void ISubject::Attach(IObserver *o) {m_observers.push_back(o); }void ISubject::Detach(IObserver *o) {m_observers.remove(o); }void ISubject::Notify(string msg) {list<IObserver *>::iterator it = m_observers.begin();while (it != m_observers.end()) {(*it)->Update(msg);++it;} }ConcreteSubject::~ConcreteSubject() { }void ConcreteSubject::setMessage(string msg) {Notify(msg); }string ConcreteSubject::getMssage() {return m_msg; }

iobserver.h

#include <string> #include <iostream>using namespace std;//抽象觀察者 class IObserver { public:virtual ~IObserver();virtual void Update(string msg) = 0; };class ConcreteObserver : public IObserver { public:ConcreteObserver(string name);void Update(string msg);private:string m_name; };

iobserver.cpp

#include "iobserver.h"IObserver::~IObserver() {}ConcreteObserver::ConcreteObserver(string name):m_name(name) { }void ConcreteObserver::Update(string msg) {cout << m_name << " Notification:"<< msg << endl; }

main.cpp

#include <iostream> #include "isubject.h" #include "iobserver.h"using namespace std; #define DELETE(x) {if(x){delete(x);(x) = nullptr;}}int main() {//創建目標和觀察者,這里觀察者為三個班級ConcreteSubject * pSubject = new ConcreteSubject();IObserver * pObserver1 = new ConcreteObserver("classA");IObserver * pObserver2 = new ConcreteObserver("classB");IObserver * pObserver3 = new ConcreteObserver("classC");//注冊觀察者pSubject->Attach(pObserver1);pSubject->Attach(pObserver2);pSubject->Attach(pObserver3);//發布通知啦pSubject->Notify("Due to the weather, today’s holiday.");//刪除一個班級pSubject->Detach(pObserver2);//再次通知pSubject->Notify("Pay the data fee.");DELETE(pSubject);DELETE(pObserver1);DELETE(pObserver2);DELETE(pObserver3);return 0; }

輸出:

classA Notification:Due to the weather, today’s holiday. classB Notification:Due to the weather, today’s holiday. classC Notification:Due to the weather, today’s holiday. classA Notification:Pay the data fee. classC Notification:Pay the data fee.

上述代碼在這里

總結

以上是生活随笔為你收集整理的C++设计模式--观察者模式(Observer)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。