设计模式---观察者模式(C++实现)
来源:互联网 发布:王者荣耀网络不稳定 编辑:程序博客网 时间:2024/05/17 23:33
#include "stdafx.h"
#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std;
//
//
// Generated by StarUML(tm) C++ Add-In
//
// @ Project : Untitled
// @ File Name : INotifier.h
// @ Date : 2016-11-8
// @ Author : richard.liu
//
//
class TecherListenner {
public:
virtual void onTecherComming() = 0;
};
class INotifier {
public:
virtual void addListenner(TecherListenner* listenner) = 0;
virtual void removeListenner(TecherListenner* removeListenner) = 0;
virtual void notify() = 0;
};
class TecherNotifier : public INotifier {
public:
list<TecherListenner*> m_listenners;
void addListenner(TecherListenner* listenner);
void removeListenner(TecherListenner* listenner);
void notify();
};
void TecherNotifier::addListenner(TecherListenner* listenner) {
m_listenners.push_back(listenner);
}
void TecherNotifier::removeListenner(TecherListenner* listenner) {
m_listenners.push_back(listenner);
}
void TecherNotifier::notify() {
list <TecherListenner*> ::iterator it = m_listenners.begin();
for (; it != m_listenners.end(); ++ it)
{
(*it)->onTecherComming();
}
}
class XiaoZhang : public TecherListenner {
public:
void onTecherComming();
void stopCopyWork();
void copyHomeWork();
};
void XiaoZhang::onTecherComming() {
stopCopyWork();
}
void XiaoZhang::stopCopyWork() {
cout << "stop copy homework" << endl;
}
void XiaoZhang::copyHomeWork() {
cout << "copy homework now" << endl;
}
int main(int argc, char* argv[])
{
TecherNotifier monitor;
XiaoZhang xz;
monitor.addListenner(&xz);
xz.copyHomeWork();
monitor.notify();
printf("Hello World!\n");
return 0;
}
0 0
- 设计模式--观察者模式(C++)
- 设计模式--观察者模式(C++)
- 设计模式--观察者模式(C++)
- 设计模式----观察者模式Observer(C++)
- 设计模式之观察者模式(C++)
- 设计模式---观察者模式(C++)
- 设计模式之观察者模式(c++)
- 设计模式-观察者模式(PHP实现)
- 设计模式---观察者模式(C++实现)
- 设计模式---观察者模式(C++实现)
- 设计模式-观察者模式(Java实现)
- 实现设计模式:观察者模式
- 设计模式之观察者模式(订阅模式),jquery实现
- Observer(观察者)设计模式代码实现
- 设计模式之观察者设计模式(C++实现)
- 设计模式(观察者模式)
- C++设计模式实现--观察者
- C++设计模式实现--观察者
- Atitit图片复制父目录给你设计的实现 基于win 图片浏览器
- 51nod 1205 流水线调度
- 数据结构和算法--栈的链式实现
- C语言性能优化——使用位运算
- 一个acca web前端开发工程师考试操作题
- 设计模式---观察者模式(C++实现)
- 【11.8】P73 T2
- SVN服务端安装
- Android清除单个域名的cookie
- [js点滴]JavaScript中Image对象操作实例
- Codeforces 401D Roman and Numbers【状压dp】
- 洛谷3195玩具装箱(斜率优化)
- 利用链接下载该页面图片
- 数据结构和算法--队列的数组实现