(ZZ) 设计模式:观察者模式
2015-12-14 14:55
459 查看
原文地址: /article/4902664.html
因为要用到观察者模式编程,翻看了一些blog。这边文章还是写得比较通俗易懂、方便理解的。
感谢原文作者!
设计模式系列目录
今天介绍另一种模式
观察者模式是我们经常用的一个模式,比如在用wcf做服务的时候通知客户端做一些操作一般用设计者模式。
今天做一个订报纸的小例子来理解一下观察者模式 出版者+订阅者=观察者模式
用下边的图来说就是 人民日报+订阅者=观察者模式
只要是订阅了人民日报的人,有了新报纸就会送到订阅者那里去,
当你不想订的时候取消就不订阅就不会再收到报纸了。
下面我们就来用代码实现一下这个模式
?
开始订订阅,和调用了
?
c++代码
View Code
View Code
View Code
调用
因为要用到观察者模式编程,翻看了一些blog。这边文章还是写得比较通俗易懂、方便理解的。
感谢原文作者!
设计模式系列目录
今天介绍另一种模式
观察者模式是我们经常用的一个模式,比如在用wcf做服务的时候通知客户端做一些操作一般用设计者模式。
今天做一个订报纸的小例子来理解一下观察者模式 出版者+订阅者=观察者模式
用下边的图来说就是 人民日报+订阅者=观察者模式
只要是订阅了人民日报的人,有了新报纸就会送到订阅者那里去,
当你不想订的时候取消就不订阅就不会再收到报纸了。
下面我们就来用代码实现一下这个模式
?
?
c++代码
#pragma once #include <iostream> //订阅者 class ISubScribe { public: //有新的报纸了就会被执行通知 virtual void HasNewPaper() = 0; virtual ~ISubScribe(){} }; //报纸接口 class INewsPaper { public: //添加订阅者 virtual void RegisterSubscriber(ISubScribe* f_subScribe) = 0; //取消订阅 virtual void RemoveSubScriber(ISubScribe* f_subScribe) = 0; //发送报纸 virtual void SendPaper() = 0; virtual ~INewsPaper(){} };
View Code
#pragma once #include "inewspaper.h" #include <iostream> #include <vector> using namespace std; class PeopleNewsPaper : public INewsPaper { public: PeopleNewsPaper(); virtual ~PeopleNewsPaper(); //添加订阅者 void RegisterSubscriber(ISubScribe* f_subScribe); //取消订阅 void RemoveSubScriber(ISubScribe* f_subScribe); //发送报纸 void SendPaper(); private: vector<ISubScribe*> subs; }; #include "PeopleNewsPaper.h" #include <algorithm> void MyClearSub(ISubScribe* sub) { if (sub) { delete sub; sub = nullptr; } } PeopleNewsPaper::PeopleNewsPaper() { } PeopleNewsPaper::~PeopleNewsPaper() { if (!subs.empty()) { for_each(subs.begin(), subs.end(), MyClearSub); subs.clear(); } } //添加订阅者 void PeopleNewsPaper::RegisterSubscriber(ISubScribe* f_subScribe) { if (subs.empty()) { subs.push_back(f_subScribe); } else if (find(subs.begin(), subs.end(), f_subScribe) == subs.end()) { subs.push_back(f_subScribe); } } //取消订阅 void PeopleNewsPaper::RemoveSubScriber(ISubScribe* f_subScribe) { vector<ISubScribe*>::iterator itor = find(subs.begin(), subs.end(), f_subScribe); if (itor != subs.end()) { subs.erase(itor); MyClearSub(f_subScribe); } } //发送报纸 void PeopleNewsPaper::SendPaper() { for (ISubScribe* sub : subs) { sub->HasNewPaper(); } }
View Code
#pragma once #include "inewspaper.h" #include <iostream> #include <string> class SubHuman : public ISubScribe { public: SubHuman(std::string name); ~SubHuman(); void HasNewPaper(); private: std::string m_name; }; #include "SubHuman.h" using namespace std; SubHuman::SubHuman(string name) { m_name = name; } SubHuman::~SubHuman() { } void SubHuman::HasNewPaper() { std::cout << m_name << "有报纸来了!!" << endl; }
View Code
调用
#include <iostream> #include "inewspaper.h" #include "PeopleNewsPaper.h" #include "SubHuman.h" int main() { INewsPaper* peopPaper = new PeopleNewsPaper(); ISubScribe* sub = new SubHuman("小李"); peopPaper->RegisterSubscriber(sub); sub = new SubHuman("小张"); peopPaper->RegisterSubscriber(sub); sub = new SubHuman("小刘"); peopPaper->RegisterSubscriber(sub); peopPaper->SendPaper(); peopPaper->RemoveSubScriber(sub); peopPaper->SendPaper(); delete peopPaper; }
相关文章推荐
- java.lang.IllegalArgumentException: Page directive: invalid value for import
- MEF
- 几道和三角函数有关的题目
- pg_dump和pg_restore
- Android sdk镜像设置
- Scala编程实例:使用Set和Map
- WebService:CXF开发
- Java类文件结构
- 老男孩教育实践:参观森华易腾的IDC机房
- 页面加载后检查用户登录状态,通过ajax实现
- yum-1-基本用法
- 低功耗模式操作
- NSString的补充
- Android源码阅读
- JAVA-适配器模式
- layout 常用属性
- docker命令-run
- 数据库设计三大范式
- jekyll 的安装
- RequireJS入门指导 (转)