惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tor Project blog
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
I
InfoQ
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
美团技术团队
B
Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
TaoSecurity Blog
TaoSecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
H
Heimdal Security Blog
爱范儿
爱范儿
博客园_首页
SecWiki News
SecWiki News
腾讯CDC
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
The Register - Security
The Register - Security
N
News | PayPal Newsroom
Recent Commits to openclaw:main
Recent Commits to openclaw:main
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Security Latest
Security Latest
A
Arctic Wolf
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
Attack and Defense Labs
Attack and Defense Labs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
C
Check Point Blog
Y
Y Combinator Blog
T
The Exploit Database - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
I
Intezer
博客园 - 叶小钗
Cisco Talos Blog
Cisco Talos Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
小众软件
小众软件

博客园 - 浪端之渡鸟

讨论exe获取dll提供的单例,并获取单例所提供的带有vector<class A>& STL容器的返回值的情况-提供1种解决方法 vs2010 mfc dll 调试编译的问题 std::stringstream 模版实现,类型转换,遇到空格不跳出 字节对齐,对opencv的影响 跨平台,c语言创建多级路径代码分享 吐槽VS2010,卡死后,一直不能点,强制关机后代码丢失 IOS App资源路径 opencv在MFC下使用的问题 fopen的打开方式 解决安装macports,不能更新的问题 坑爹啊,新手一个这问题折腾2天,xcode4.5拖上去控件真机调试就崩 静态页面转化 删除_svn文件夹 开发相关日志 WebClient.cs封装 symbian 输入控件不显示输入法解决: V3 V5宏定义 coco2d-x 重置z轴 htmlcontrol-for-symbian 源码解析
老鼠跑猫叫主人惊醒c++观察者模式实现
浪端之渡鸟 · 2013-09-06 · via 博客园 - 浪端之渡鸟

这个题目算是比较经典的观察者模式了,老鼠作为一个Subject,主动发出跑的动作,紧跟着猫由于老鼠的跑而发出叫声,主人也被惊醒,在这里猫跟主人都是被动的,是观察者角色,代码实现如下:

 1 class CSubject;
 2 //观察者
 3 class CObserver
 4 {
 5 public:
 6     CObserver(){}
 7     virtual ~CObserver(){}
 8     virtual void Update(CSubject* pSubject) = 0;
 9 };
10 
11 //目标即主题,可理解为由于本对象变化导致其他对象跟随变化
12 class CSubject
13 {
14 public:
15     void Attach(CObserver* pO)
16     {
17         _ls.push_back(pO);
18     }
19     void Detach(CObserver* pO)
20     {
21         _ls.remove(pO);
22     }
23     void Notify()
24     {
25         for (list<CObserver*>::iterator it=_ls.begin();it!=_ls.end();it++)
26         {
27             (*it)->Update(this);
28         }
29     }
30 private:
31     list<CObserver*> _ls;
32 };
33 
34 //猫为观察者
35 class CCat:public CObserver
36 {
37 public:
38     CCat(){}
39     virtual ~CCat(){}
40     virtual void Update(CSubject* pSubject){
41         Say();
42     }
43     void Say()
44     {
45         printf("猫叫了\r\n");
46     }
47 };
48 
49 //人为观察者
50 class CPerson:public CObserver
51 {
52     public:
53         CPerson(){}
54         virtual ~CPerson(){}
55 
56     virtual void Update(CSubject* pSubject){
57         Say();
58     }
59     void Say()
60     {
61         printf("人醒了\r\n");
62     }
63 };
64 
65 //老鼠为主题
66 class CMouse:public CSubject
67 {
68 public:
69 
70     void Say()
71     {
72         printf("老鼠叫了\r\n");
73         Notify();
74     }
75 };
 1 int _tmain(int argc, _TCHAR* argv[])
 2 {
 3 
 4     //老鼠跑猫叫主人醒
 5     CMouse* pMouse = new CMouse();
 6     CCat* pCat = new CCat();
 7     CPerson* pPerson = new CPerson();
 8 
 9     pMouse->Attach(pCat);
10     pMouse->Attach(pPerson);
11     pMouse->Say();
12   
13     return 0;
14 }

main里指针没做释放,如何优雅的释放呢,还在考虑中,如果讲究对称美,那就是

 1 int _tmain(int argc, _TCHAR* argv[])
 2 {
 3 
 4     //老鼠跑猫叫主人醒
 5     CMouse* pMouse = new CMouse();
 6     CCat* pCat = new CCat();
 7     CPerson* pPerson = new CPerson();
 8 
 9     pMouse->Attach(pCat);
10     pMouse->Attach(pPerson);
11     pMouse->Say();
12 
13     //对称美,但是如此麻烦,还有其他解决方法吗
14     pMouse->Detach(pCat);
15     pMouse->Detach(pPerson);
16     delete pPerson;
17     delete pCat;
18     delete pMouse;
19     
20 
21     return 0;
22 }

欢迎各位发表观点