1 // ADBHelper.cpp : This file contains the 'main' function. Program execution begins and ends there.
2 //
3
4 //#include "pch.h"
5 #include <stdafx.h>
6 #include <iostream>
7 #include <list>
8 using namespace std;
9
10 class Observer
11 {
12 public:
13 virtual void Update(int) = 0;
14 };
15
16 class Subject
17 {
18 public:
19 virtual void Attach(Observer *) = 0;
20 virtual void Detach(Observer *) = 0;
21 virtual void Notify() = 0;
22 };
23
24 class ConcreteObserver : public Observer
25 {
26 public:
27 ConcreteObserver(Subject *pSubject) : m_pSubject(pSubject) {}
28
29 void Update(int value)
30 {
31 cout << "ConcreteObserver get the update. New State:" << value << endl;
32 }
33
34 private:
35 Subject *m_pSubject;
36 };
37
38 class ConcreteObserver2 : public Observer
39 {
40 public:
41 ConcreteObserver2(Subject *pSubject) : m_pSubject(pSubject) {}
42
43 void Update(int value)
44 {
45 cout << "ConcreteObserver2 get the update. New State:" << value << endl;
46 }
47
48 private:
49 Subject *m_pSubject;
50 };
51
52 class ConcreteSubject : public Subject
53 {
54 public:
55 void Attach(Observer *pObserver);
56 void Detach(Observer *pObserver);
57 void Notify();
58
59 void SetState(int state)
60 {
61 m_iState = state;
62 }
63
64 private:
65 std::list<Observer *> m_ObserverList;
66 int m_iState;
67 };
68
69 void ConcreteSubject::Attach(Observer *pObserver)
70 {
71 m_ObserverList.push_back(pObserver);
72 }
73
74 void ConcreteSubject::Detach(Observer *pObserver)
75 {
76 m_ObserverList.remove(pObserver);
77 }
78
79 void ConcreteSubject::Notify()
80 {
81 std::list<Observer *>::iterator it = m_ObserverList.begin();
82 while (it != m_ObserverList.end())
83 {
84 (*it)->Update(m_iState);
85 ++it;
86 }
87 }
88
89 int main()
90 {
91 // Create Subject
92 ConcreteSubject *pSubject = new ConcreteSubject();
93
94 // Create Observer
95 Observer *pObserver = new ConcreteObserver(pSubject);
96 Observer *pObserver2 = new ConcreteObserver2(pSubject);
97
98 // Change the state
99 pSubject->SetState(2);
100
101 // Register the observer
102 pSubject->Attach(pObserver);
103 pSubject->Attach(pObserver2);
104
105 pSubject->Notify();
106
107 // Unregister the observer
108 pSubject->Detach(pObserver);
109
110 pSubject->SetState(3);
111 pSubject->Notify();
112
113 delete pObserver;
114 delete pObserver2;
115 delete pSubject;
116 }
117
118 // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
119 // Debug program: F5 or Debug > Start Debugging menu
120
121 // Tips for Getting Started:
122 // 1. Use the Solution Explorer window to add/manage files
123 // 2. Use the Team Explorer window to connect to source control
124 // 3. Use the Output window to see build output and other messages
125 // 4. Use the Error List window to view errors
126 // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
127 // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

转载:https://www.cnblogs.com/carsonzhu/p/5770253.html

未完,待描述……

设计模式之观察者模式_C++的更多相关文章

  1. 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)

    原文:乐在其中设计模式(C#) - 观察者模式(Observer Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 观察者模式(Observer Pattern) 作者:weba ...

  2. 设计模式之观察者模式(Observable与Observer)

    设计模式之观察者模式(Observable与Observer) 好久没有写博客啦,之前看完了<设计模式之禅>也没有总结一下,现在回忆一下设计模式之观察者模式. 1.什么是观察者模式 简单情 ...

  3. 8.5 GOF设计模式四: 观察者模式Observer

    GOF设计模式四: 观察者模式Observer  现实中遇到的问题  当有许多不同的客户都对同一数据源感兴趣,对相同的数据有不同的处理方式,该如 何解决?5.1 定义: 观察者模式  观察者模式 ...

  4. php 设计模式之观察者模式(订阅者模式)

    php 设计模式之观察者模式 实例 没用设计模式的代码,这样的代码要是把最上面那部分也要符合要求加进来,就要修改代码,不符合宁增不改的原则 介绍 观察者模式定义对象的一对多依赖,这样一来,当一个对象改 ...

  5. [JS设计模式]:观察者模式(即发布-订阅者模式)(4)

    简介 观察者模式又叫发布---订阅模式,它定义了对象间的一种一对多的关系,让多个观察者对象同时监听某一个主题对象,当一个对象发生改变时,所有依赖于它的对象都将得到通知. 举一个现实生活中的例子,例如小 ...

  6. 实践GoF的23种设计模式:观察者模式

    摘要:当你需要监听某个状态的变更,且在状态变更时通知到监听者,用观察者模式吧. 本文分享自华为云社区<[Go实现]实践GoF的23种设计模式:观察者模式>,作者: 元闰子 . 简介 现在有 ...

  7. java设计模式之观察者模式

    观察者模式 观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式.模型-视图(View)模式.源-收听者(Listener)模式或从属者模式)是软件设计模式的一种.在此种模 ...

  8. [python实现设计模式]-4.观察者模式-吃食啦!

    观察者模式是一个非常重要的设计模式. 我们先从一个故事引入. 工作日的每天5点左右,大燕同学都会给大家订饭. 然后7点左右,饭来了. 于是燕哥大吼一声,“饭来啦!”,5点钟定过饭的同学就会纷纷涌入餐厅 ...

  9. 【GOF23设计模式】观察者模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_观察者模式.广播机制.消息订阅.网络游戏对战原理 package com.test.observer; import ja ...

随机推荐

  1. DML添加数据&删除数据&修改数据

    DML:增删改表中数据 1.添加数据: 语法:insert into 表名(列名1,列名2).... values(值1,值2): 注意: 1.列名和值要一一对应. 2.如果表名后,不定义列名,则默认 ...

  2. ES6中class方法及super关键字

    ES6 class中的一些问题 记录下class中的原型,实例,super之间的关系 //父类 class Dad { constructor(x, y) { this.x = 5; this.y = ...

  3. javascript原生style属性分析

    1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="U ...

  4. python编程思想及对象与类

    目录 编程思想 面向对象 面向过程 对象与类的概念 对象与类的创建 对象的实例化方法-独有数据 编程思想 1.面向对象 1.1. 面向对象前戏 案例:人狗大战 # 需求:人狗大战# 1.'创造'出人和 ...

  5. 小白之Python基础(三)

    列表和元组 1.列表:最常用的 Python 数据类型(可变的数据类型) 1)列表是一个值,它包含多个值构成的序列: 2)通过[ ]或list()创建的有序元素的集合: 3)表项(列表中的值,也可以叫 ...

  6. 一文搞懂什么是kubernetes Service

    1.什么是Service? 在kubernets中,Pod是应用程序的载体,Pod你可以想象成就是容器,为动态的一组Pod提供一个固定的访问入口,它是以一种叫ClusterIP地址来进行标识,而Clu ...

  7. java反射的初理解

    反射 获取类的方法: Class<?> aClass1 = Class.forName("TestDemo.refection.User");//通过类路径获取 Cla ...

  8. 万答#15,都有哪些情况可能导致MGR服务无法启动

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 本文转载自微信公众号 "老叶茶馆" 欢迎大家关注! 1.都有 ...

  9. 毕昇编译器优化:Lazy Code Motion

    摘要:本文中,我们将介绍通过代码移动(插入)的方式消除冗余计算的一个典型方法. 本文分享自华为云社区<编译器优化那些事儿(3):Lazy Code Motion>,作者:毕昇小助手. 导语 ...

  10. Luogu[YNOI2019]排序(DP,线段树)

    要最优?就要一步到位,不能做"马后炮",走"回头路",因此将序列映射到一个假定最优序列,发现移动原序列等价于删除原序列元素,以便生成最大不下降子序列.可线段树维 ...