发布订阅者模式C++实现
2012-10-08 17:26
543 查看
#ifndef BOTTON_H
#define BOTTON_H
#include <string>
#include <vector>
class BottonListener;
class Botton
{
public:
Botton(const std::string & botton_name);
~Botton();
const std::string & GetName() const;
void AddBottonListener(BottonListener * botton_listener);
void RemoveBottonListenner(BottonListener * botton_listener);
void Run();
private:
typedef std::vector<BottonListener*>::iterator ITER;
std::string name_;
std::vector<BottonListener*> listener_vector_;
};
#endif/*BOTTON_H*/
#ifndef BOTTON_LISTENER_H
#define BOTTON_LISTENER_H
class Botton;
class BottonListener
{
public:
BottonListener();
virtual ~BottonListener();
virtual void BottonClick(Botton * botton) = 0;
};
#endif/*BOTTON_LISTENER_H*/
#ifndef MY_BOTTON_LISTENER_H
#define MY_BOTTON_LISTENER_H
#include "botton_listener.h"
#include "botton.h"
class MyBottonListener : public BottonListener
{
public:
MyBottonListener(int number);
~MyBottonListener();
void BottonClick(Botton * botton);
private:
int number_;
};
#endif/*MY_BOTTON_LISTENER_H*/
#include "botton_listener.h"
#include "botton.h"
Botton::Botton(const std::string &botton_name) : name_(botton_name)
{
}
Botton::~Botton()
{
}
const std::string & Botton::GetName() const
{
return this->name_;
}
void Botton::AddBottonListener(BottonListener * botton_listener)
{
this->listener_vector_.push_back(botton_listener);
}
void Botton::RemoveBottonListenner(BottonListener * botton_listener)
{
bool key = false;
ITER iter;
for(iter = listener_vector_.begin();iter != listener_vector_.end();iter++)
{
if(*iter == botton_listener )
{
key = true;
break;
}
}
if(key)
{
listener_vector_.erase(iter);
}
}
void Botton::Run()
{
for(ITER iter = listener_vector_.begin(); iter != listener_vector_.end(); iter++)
{
BottonListener * p_botton_listener = *iter;
p_botton_listener->BottonClick(this);
}
}
#include "botton_listener.h"
BottonListener::BottonListener()
{
}
BottonListener::~BottonListener()
{
}
#include <iostream>
#include "my_botton_listener.h"
MyBottonListener::MyBottonListener(int number) : number_(number)
{
}
MyBottonListener::~MyBottonListener()
{
}
void MyBottonListener::BottonClick(Botton *botton)
{
std::cout<<botton->GetName()<<" "<<number_<<std::endl;
}
#include "botton_listener.h"
#include "botton.h"
#include "my_botton_listener.h"
int main(int argc,char **argv)
{
Botton *botton = new Botton("Hello!");
BottonListener *botton_listener_1 = new MyBottonListener(1);
BottonListener *botton_listener_2 = new MyBottonListener(2);
botton->AddBottonListener(botton_listener_1);
botton->AddBottonListener(botton_listener_2);
botton->Run();
botton->RemoveBottonListenner(botton_listener_1);
botton->RemoveBottonListenner(botton_listener_2);
delete botton;
delete botton_listener_1;
delete botton_listener_2;
}
原文地址: http://blog.csdn.net/zhangbohtz/article/details/6941381
#define BOTTON_H
#include <string>
#include <vector>
class BottonListener;
class Botton
{
public:
Botton(const std::string & botton_name);
~Botton();
const std::string & GetName() const;
void AddBottonListener(BottonListener * botton_listener);
void RemoveBottonListenner(BottonListener * botton_listener);
void Run();
private:
typedef std::vector<BottonListener*>::iterator ITER;
std::string name_;
std::vector<BottonListener*> listener_vector_;
};
#endif/*BOTTON_H*/
#ifndef BOTTON_LISTENER_H
#define BOTTON_LISTENER_H
class Botton;
class BottonListener
{
public:
BottonListener();
virtual ~BottonListener();
virtual void BottonClick(Botton * botton) = 0;
};
#endif/*BOTTON_LISTENER_H*/
#ifndef MY_BOTTON_LISTENER_H
#define MY_BOTTON_LISTENER_H
#include "botton_listener.h"
#include "botton.h"
class MyBottonListener : public BottonListener
{
public:
MyBottonListener(int number);
~MyBottonListener();
void BottonClick(Botton * botton);
private:
int number_;
};
#endif/*MY_BOTTON_LISTENER_H*/
#include "botton_listener.h"
#include "botton.h"
Botton::Botton(const std::string &botton_name) : name_(botton_name)
{
}
Botton::~Botton()
{
}
const std::string & Botton::GetName() const
{
return this->name_;
}
void Botton::AddBottonListener(BottonListener * botton_listener)
{
this->listener_vector_.push_back(botton_listener);
}
void Botton::RemoveBottonListenner(BottonListener * botton_listener)
{
bool key = false;
ITER iter;
for(iter = listener_vector_.begin();iter != listener_vector_.end();iter++)
{
if(*iter == botton_listener )
{
key = true;
break;
}
}
if(key)
{
listener_vector_.erase(iter);
}
}
void Botton::Run()
{
for(ITER iter = listener_vector_.begin(); iter != listener_vector_.end(); iter++)
{
BottonListener * p_botton_listener = *iter;
p_botton_listener->BottonClick(this);
}
}
#include "botton_listener.h"
BottonListener::BottonListener()
{
}
BottonListener::~BottonListener()
{
}
#include <iostream>
#include "my_botton_listener.h"
MyBottonListener::MyBottonListener(int number) : number_(number)
{
}
MyBottonListener::~MyBottonListener()
{
}
void MyBottonListener::BottonClick(Botton *botton)
{
std::cout<<botton->GetName()<<" "<<number_<<std::endl;
}
#include "botton_listener.h"
#include "botton.h"
#include "my_botton_listener.h"
int main(int argc,char **argv)
{
Botton *botton = new Botton("Hello!");
BottonListener *botton_listener_1 = new MyBottonListener(1);
BottonListener *botton_listener_2 = new MyBottonListener(2);
botton->AddBottonListener(botton_listener_1);
botton->AddBottonListener(botton_listener_2);
botton->Run();
botton->RemoveBottonListenner(botton_listener_1);
botton->RemoveBottonListenner(botton_listener_2);
delete botton;
delete botton_listener_1;
delete botton_listener_2;
}
原文地址: http://blog.csdn.net/zhangbohtz/article/details/6941381
相关文章推荐
- 发布订阅者模式c++实现
- 发布订阅者模式之C#委托实现
- 订阅发布模式——C++实现
- 发布订阅者模式之C#委托实现
- ActiveMQ 快速入门教程系列 第二章 发布-订阅者模式实现
- Head First设计模式C++实现--第十章:状态(State)模式
- 最常用的设计模式---适配器模式(C++实现)
- 单例模式C++实现
- 数据挖掘-关联分析频繁模式挖掘Apriori、FP-Growth及Eclat算法的JAVA及C++实现
- 设计模式—装饰模式的C++实现
- C#实现发布订阅模式
- 设计模式之观察者(Observer)模式与其C++通用实现(中)
- 设计模式之适配器模式(C++实现)
- 设计模式C++实现(4)——单例模式
- 原型模式C++实现
- c++中单体模式的实现
- 常见设计模式解析和实现(C++)FlyWeight模式
- 在C++中使用事件回调机制(通过Observer模式、函数指针等实现)
- 命令模式c++实现
- 面试题1:如何实现C++单例模式?