前提:在上一篇文章中,自己简单写了一个注册回调的例子(c++ 注册回调函数研究),以及自己对注册回调一些理解,在后面的研究过程中,特别是做项目的时候越来越发现注册回调在项目中占有非常重要的重用,在目前自己所做的项目中,基本上每一个模块都使用了注册回调,所以自己在以现在所做项目为背景又写了一个简单的例子,过程写的有些粗糙,主要是为了实现这种架构模式。
代码 实例背景:在一个基类的基础上诞生了两个子类,分别是分别是视频类(Video) 和音频类(Audio),在音频和视频切换的时候需要做相关显示界面的切换,以及相关播放视频控制的切换。实现切换的过程在基类中实现。
代码示例:
(0) 实现监听程序
#pragma once
#include<list>
class Listener
{
public:
Listener(){};
~Listener(){};
virtual void update(const int &mask, const void *message) = 0;
};
class Subject
{
public:
Subject()
{
}
~ Subject()
{
}
void addNotifyListener(Listener *listener)
{
if(listener != nullptr)
{
m_Listeners.push_back(listener);
}
}
void removeListener(Listener *listener)
{
if(listener != nullptr)
{
m_Listeners.remove(listener);
}
}
void clearListener()
{
LISTENERLIST::iterator it = m_Listeners.begin();
LISTENERLIST::iterator end = m_Listeners.end();
while (it!= end)
{
Listener* l = *it;
delete l;
l = nullptr;
it++;
}
m_Listeners.clear();
}
void notify(const int &mask, const void *message = nullptr)
{
LISTENERLIST::iterator it = m_Listeners.begin();
LISTENERLIST::iterator end = m_Listeners.end();
while (it!= end)
{
(*it)->update(mask,message);
it++;
}
}
private:
typedef std::list<Listener *> LISTENERLIST;
LISTENERLIST m_Listeners;
};
(1) 首先定义全局消息 ,(Message.h 文件)
#pragma once
enum
{
SHOW_VIDOE = 2,
SHOW_AUIDO = 3,
};
(2) 定义基类文件 (parentA.hpp)
#pragma once
#include"stdafx.h"
#include<iostream>
#include"Listener.h"
#include"Messsage.h"
class ParentA:public Subject
{
public:
void showParentAll(int value) //
{
if(value == 1)
{
this->notify(SHOW_AUIDO);
}
else if(value ==2)
{
this->notify(SHOW_VIDOE);
}
//std::cout<<"This is Parent"<<std::endl;
}
private:
};
(3) 定义两个子类文件
// ChildA_1
#pragma once
#include"stdafx.h"
#include"parentA.hpp"
#include"Messsage.h"
class ChildA_1 :public ParentA
{
public:
ChildA_1()
{
}
~ ChildA_1()
{
}
void showChildAudio()
{
//
}
private:
};
//ChildA_2
#pragma once
#include"stdafx.h"
#include"parentA.hpp"
#include"Messsage.h"
class ChildA_1 :public ParentA
{
public:
ChildA_1()
{
}
~ ChildA_1()
{
}
void showChildAudio()
{
//
}
private:
};
(4) 定义调用用程序
#pragma once
#include"stdafx.h"
#include"parentA.hpp"
#include"ChildA_1.hpp"
#include"ChildA_2.hpp"
#include"Listener.h"
using namespace std;
class MainWindow:public Listener
{
public:
MainWindow()
{
m_childAuido = new ChildA_1();
m_childVideo = new ChildA_2();
m_childAuido->addNotifyListener(this);
m_childVideo->addNotifyListener(this);
}
~MainWindow()
{
}
void update(const int &mask, const void *message)
{
if(mask == SHOW_AUIDO)
{
cout<<"AUdio"<<endl;
}
else if(mask == SHOW_VIDOE)
{
cout<<"VIdeo" <<endl;
}
}
void TestListener()
{
for(int i =1;i < 4 ; i++)
{
if( i == 1 )
{
m_parent = m_childAuido;
m_parent->showParentAll(i);
}
else if(i ==2)
{
m_parent = m_childVideo;
m_parent->showParentAll(i);
}
}
}
private:
ParentA *m_parent;
ChildA_1 *m_childAuido;
ChildA_2 *m_childVideo;
};
(测试主程序)
#include "stdafx.h"
#include"MainWindowcpp.cpp"
int _tmain(int argc, _TCHAR* argv[])
{
MainWindow m_mainWindow;
m_mainWindow.TestListener();
system("Pause");
return 0;
}
总结:上面的过程比较简单,主要是想把主要的设计思想体现出来,望大家见谅。