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. PHP单粒模式

    <?php class C { //三私一公 protected static $_instance = null; protected function __construct() //pro ...

  2. AI+医疗:使用神经网络进行医学影像识别分析 ⛵

    作者:韩信子@ShowMeAI 计算机视觉实战系列:https://www.showmeai.tech/tutorials/46 行业名企应用系列:https://www.showmeai.tech/ ...

  3. 红黑树以及JAVA实现(一)

    目录 前言 一. B树 1.1 概念 1.2 2-3-4树 1.3 2-3-4树的插入 节点分类 1.4 2-3-4树的删除 1.4.1 当删除节点是叶子节点 1.4.1.1 当删除节点为非2节点 1 ...

  4. kubernetes网络排错思想

    Overview 本文将引入一个思路:"在Kubernetes集群发生网络异常时如何排查".文章将引入Kubernetes 集群中网络排查的思路,包含网络异常模型,常用工具,并且提 ...

  5. RabbitMQ 入门系列:3、基础编码:官方SDK的引用、链接创建、单例改造、发送消息、接收消息。

    系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...

  6. 从零开始Blazor Server(14)--修改密码

    目前,我们只做了在用户管理里强行修改密码,而没有做用户自行修改密码的功能,今天我们来实现它. 首先,我们的用户密码修改最好的位置应该就是在头像下面的下拉菜单里,所以我们在那里的LinkTemplate ...

  7. 「JOI 2015 Final」分蛋糕 2

    「JOI 2015 Final」分蛋糕 2 题解 这道题让我想起了新年趣事之红包这道DP题,这道题和那道题推出来之后的做法是一样的. 我们可以定义dp[i][len][1] 表示从第i块逆时针数len ...

  8. Android同屏、摄像头RTMP推送常用的数据接口设计探讨

    前言 好多开发者在调用Android平台RTMP推送或轻量级RTSP服务接口时,采集到的video数据类型多样化,如420sp.I420.yv12.nv21.rgb的,还有的拿到的图像是倒置的,如果开 ...

  9. Java开发学习(三十)----Maven聚合和继承解析

    一.聚合 分模块开发后,需要将这四个项目都安装到本地仓库,目前我们只能通过项目Maven面板的install来安装,并且需要安装四个,如果我们的项目足够多,那么一个个安装起来还是比较麻烦的 如果四个项 ...

  10. KingbaseES 数据库软件卸载

    关键字: KingbaseES.卸载   一.安装后检查 在安装完成后,可以通过以下几种方式进行安装正确性验证: 1. 查看安装日志,确认没有错误记录; 2. 查看开始菜单: 查看应用程序菜单中是否安 ...