当前位置: 首页 > news >正文

设计模式C++实现11:观察者模式

参考大话设计模式;

详细内容参见大话设计模式一书第十四章,该书使用C#实现,本实验通过C++语言实现。

观察者模式又叫做发布-订阅(Publish/Subscribe)模式。

观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。[DP] 

一 学习测试代码 

书上的模式学习图

 我自己的测试UML图

测试代码:

#include <iostream>
#include <list>
using namespace std;class guanchazhe{
public:virtual void update() = 0;
};class xiaozhang :public guanchazhe{
public:virtual void update(){cout << "小张收到" << endl;}
};class xiaowang :public guanchazhe{
public:virtual void update(){cout << "小王收到" << endl;}
};class tongzhizhe{
protected:list<guanchazhe*> gcz_list;
public:void attach(guanchazhe *gcz){gcz_list.push_back(gcz);}void detech(guanchazhe *gcz){gcz_list.remove(gcz);}virtual void Notify(){list<guanchazhe*>::iterator it;for(it = gcz_list.begin();it != gcz_list.end();it++){guanchazhe *gcz = (guanchazhe *)*it;gcz->update();}}
};class laoban:public tongzhizhe
{
public:virtual void Notify(){cout << "我是老板" << endl;tongzhizhe::Notify();}
};class mishu:public tongzhizhe
{
public:virtual void Notify(){cout << "我是秘书" << endl;tongzhizhe::Notify();}
};int main(void)
{tongzhizhe *t1 = new laoban();tongzhizhe *t2 = new mishu();xiaozhang *xz = new xiaozhang();xiaowang *xw = new xiaowang();t1->attach(xz);t1->attach(xw);t1->Notify();t2->attach(xz);t2->attach(xw);t2->Notify();return 0;
}

 运行结果:

我是老板
小张收到
小王收到
我是秘书
小张收到
小王收到

二 观察者模式

我不是很喜欢这种命名,不够直观,不利于思路整理,初学还是guanchazhe,tongzhizhe这种看似很土,但是却比较容易理清思路。

观察者模式(Observer)结构图

 在上面测试中,类tongzhizhe对应Subject.
类guanchazhe对应Observer.
类laoban和xiaowang对应ConcreteSubject
类mishu和xiaowang对应ConcreteObserver.

测试代码略

三 使用函数指针模拟委托功能探讨

看书上委托的定义

 用C++来实现这两张图中的Update

1 .定义一个类叫EventHandler

2.在EventHandler中重载+运算中

3.定义个函数指针类型

4.在EventHandler定义一个链表。

聪明的你已经猜出该怎么做了吧

代码实现如下所示,写的时候发现对象的成员函数是不能直接赋值给指针的。

假设:xz.quchifan可以赋值给p1,那么p1()执行后,

cout << this->name <<  "去吃饭" << endl;

 这条代码被执行,因为没有对象调用,所示this为空this->name 使用空指针调用,段错误

证明:非静态函数不能赋值给指针。

使用函数指针实现委托的计划破产。但是从中还是可以学习C++的函数指针,重载的灵活用法。

#include <iostream>
#include <list>
using namespace std;typedef void (*update_pointer)(void);class xiaozhang{
public:string name;void quchifan(void)//去吃饭{cout << this->name <<  "去吃饭" << endl;}
};class baoan
{
public:static void quxunluo(void)//去巡逻{cout << "去巡逻" << endl;}
};class EventHandler{
public:EventHandler(){}EventHandler& operator+(update_pointer p){func_list.push_back(p);return *this;}list<update_pointer> func_list;void execute(){list<update_pointer>::iterator it;for(it = func_list.begin();it != func_list.end();it++){(*it)();}}};class tongzhizhe{
public:EventHandler update;tongzhizhe(){//update = new EventHandler();}};
int main(void)
{tongzhizhe *t = new tongzhizhe();xiaozhang xz;//update_pointer p1 = (update_pointer)(xz.quchifan);baoan ba;t->update = (t->update) + (update_pointer)(ba.quxunluo);t->update.execute();return 0;
}

运行结果:

去巡逻

小结

http://www.lryc.cn/news/5282.html

相关文章:

  • l1和l2接口如何进行编写?一定要掌握这几个元素
  • GAMES101作业7及课程总结(重点实现多线程加速,微表面模型材质)
  • 面试题(二十四)数据结构与算法
  • 【HAL库】STM32CubeMX开发----STM32F407----Uart串口接收空闲中断
  • Qt_文件操作
  • int和Integer有什么区别?
  • Axure 9 收录不同效果的制作过程
  • [Datawhale][CS224W]图神经网络(一)
  • 【Android实现16位灰度图数据转RGB数据并以bitmap格式显示】
  • uni-app②
  • FFmpeg视频处理
  • FreeRTOS任务通知 | FreeRTOS十二
  • CentOS搭建博客typecho
  • 湖南中创教育PMP如何实施风险应对,避免产生投诉
  • Urho3D子系统
  • 无线网络术语总结
  • 海卡和海派有什么区别
  • vue3学习资料整理
  • Linux基础语法进阶版
  • 近红外染料标记小分子1628790-37-3,Cyanine5.5 alkyne,花青素CY5.5炔基
  • 洛谷——P1004 方格取数
  • Linux删除软链接
  • 【自然语言处理】【大模型】用于大型Transformer的8-bit矩阵乘法介绍
  • 设计模式之工厂模式详解和应用
  • ArcGIS中的附件功能
  • epoll单台设备支持百万并发连接
  • 网络字节序
  • 03- SVC 支持向量机做人脸识别 (项目三)
  • 浅谈指向二维数组元素的指针变量
  • 左右值引用和移动语义