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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 浪端之渡鸟

讨论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 }

欢迎各位发表观点