16,548
社区成员




#include "stdafx.h"
#include <map>
using namespace std;
//设备接口抽象类,定义设备接口
class Device
{
public:
virtual void print1() = 0;
virtual void print2() = 0;
public:
Device(){}
~Device(){}
};
//具体设备,Device1
class Device1:public Device
{
public:
void print1(){ printf("Device1:print1\n");}
void print2(){ printf("Device1:print2\n");}
public:
Device1(){}
~Device1(){}
};
//具体设备,Device2
class Device2:public Device
{
public:
void print1(){printf("Device2:print1\n");}
void print2(){printf("Device2:print2\n");}
public:
Device2(){}
~Device2(){}
};
//设备类中方法类型定义
enum FUNTYPE{FUN1,FUN2};
//设备类的种类定义
enum DEVICETYPE{DEVICE1,DEVICE2};
//设备类接口函数指针变量定义
typedef void (Device::*pFUN)();
//设备类结构体
typedef struct
{
Device * pdevice; //设备类接口指针
map<FUNTYPE,pFUN> DeviceFunmap; //设备类对应的成员函数指针和成员函数类型映射
}DEVICESTRUCT;
//生成设备调用的工厂类
class ClassFacotry
{
public:
//导入所有设备的种类和每个设备类的接口函数指针
static void LoadClass()
{
//生成具体Device1结构1
DEVICESTRUCT deivestruct1;
//产生Device1
deivestruct1.pdevice = new Device1();
//把Device1的函数指针保存到map
deivestruct1.DeviceFunmap.insert(pair<FUNTYPE,pFUN>(FUN1,&Device::print1));
deivestruct1.DeviceFunmap.insert(pair<FUNTYPE,pFUN>(FUN2,&Device::print2));
//保存Device1结构1到成员map
devicemap.insert(pair<DEVICETYPE,DEVICESTRUCT>(DEVICE1,deivestruct1));
//生成具体Device2结构2
DEVICESTRUCT deivestruct2;
//产生Device2
deivestruct2.pdevice = new Device2();
//把Device1的函数指针保存到map
deivestruct2.DeviceFunmap.insert(pair<FUNTYPE,pFUN> (FUN1,&Device::print1));
deivestruct2.DeviceFunmap.insert(pair<FUNTYPE,pFUN>(FUN2,&Device::print2));
//保存Device1结构1到成员map
devicemap.insert(pair<DEVICETYPE,DEVICESTRUCT> (DEVICE2,deivestruct2));
}
static void Execute(DEVICETYPE deivetype,FUNTYPE funtype)
{
static LoadFlag =false;
if (false ==LoadFlag)
{
//导入所有设备的种类和每个设备类的接口函数指针
//只导入一次
LoadClass();
LoadFlag = true;
}
//根据设备类型和设备方法类型生成具体设备方法条用
DEVICESTRUCT deivestruct = (devicemap.find(deivetype))->second;
Device * pdevice = deivestruct.pdevice;
DeviceFun = ((deivestruct.DeviceFunmap).find(funtype))->second;
//具体设备方法条用
(pdevice->*DeviceFun)();
}
public:
ClassFacotry(){}
~ClassFacotry(){}
private:
//设备成员函数指针
static pFUN DeviceFun;
//设备类结构map
static map<DEVICETYPE,DEVICESTRUCT> devicemap;
};
//初始化设备成员函数指针
pFUN ClassFacotry::DeviceFun =NULL;
//初始化设备类结构map
map<DEVICETYPE,DEVICESTRUCT> ClassFacotry::devicemap;
int main(int argc, char* argv[])
{
//调用Device1实例的print1方法
ClassFacotry::Execute(DEVICE1,FUN1);
//调用Device2实例的print1方法
ClassFacotry::Execute(DEVICE2,FUN1);
//调用Device1实例的print2方法
ClassFacotry::Execute(DEVICE1,FUN2);
//调用Device2实例的print2方法
ClassFacotry::Execute(DEVICE2,FUN2);
return 0;
}
#include "stdafx.h"
#include <map>
using namespace std;
//设备接口抽象类,定义设备接口
class Device
{
public:
virtual void print1() = 0;
virtual void print2() = 0;
public:
Device(){}
~Device(){}
};
//具体设备,Device1
class Device1:public Device
{
public:
void print1(){ printf("Device1:print1\n");}
void print2(){ printf("Device1:print2\n");}
public:
Device1(){}
~Device1(){}
};
//具体设备,Device2
class Device2:public Device
{
public:
void print1(){printf("Device2:print1\n");}
void print2(){printf("Device2:print2\n");}
public:
Device2(){}
~Device2(){}
};
//##########################################################
//设备类中方法类型定义
enum FUNTYPE{FUN1,FUN2};
//设备类的种类定义
enum DEVICETYPE{DEVICE1,DEVICE2};
//#########################################################
//#######################################################################
//万能接口工厂类宏定义
//=====================================================================
//开始创建工厂类宏定义
#define BEGIN_CREATE_CLASS(_baseclass) \
typedef void (_baseclass::*pFUN)(); \
\
typedef struct \
{ \
_baseclass * pdevice; \
map<FUNTYPE,pFUN> DeviceFunmap; \
}_baseclass##CLASSSTRUCT; \
\
class _baseclass##Facotry \
{ \
public: \
static void LoadClass() \
{
//==============================================================================
//==============================================================================
//开始对象-对象类型映射宏定义
#define ON_CREATING_CLASS(_derivedclsstype,_derivedclass,_baseclass) \
do{ \
_baseclass##CLASSSTRUCT deivestruct; \
deivestruct.pdevice = new _derivedclass##(); \
deivestruct.DeviceFunmap.insert(pair<FUNTYPE,pFUN>(FUN1,&Device::print1)); \
deivestruct.DeviceFunmap.insert(pair<FUNTYPE,pFUN>(FUN2,&Device::print2)); \
devicemap.insert(pair<DEVICETYPE,_baseclass##CLASSSTRUCT>(_derivedclsstype,deivestruct));\
}while(0);
//==============================================================================
//==============================================================================
//工厂类定义结束
#define END_CREATE_CLASS(_baseclass) \
} \
static void Execute(DEVICETYPE deivetype,FUNTYPE funtype) \
{ \
static LoadFlag =false; \
if (false ==LoadFlag) \
{ \
LoadClass(); \
LoadFlag = true; \
} \
\
_baseclass##CLASSSTRUCT deivestruct = (devicemap.find(deivetype))->second;\
Device * pdevice = deivestruct.pdevice; \
DeviceFun = ((deivestruct.DeviceFunmap).find(funtype))->second;\
\
(pdevice->*DeviceFun)(); \
\
} \
\
public: \
_baseclass##Facotry(){} \
~_baseclass##Facotry(){} \
\
private: \
static pFUN DeviceFun; \
\
static map<DEVICETYPE,_baseclass##CLASSSTRUCT> devicemap; \
}; \
\
pFUN _baseclass##Facotry::DeviceFun =NULL; \
map<DEVICETYPE,_baseclass##CLASSSTRUCT> _baseclass##Facotry::devicemap;
//==============================================================================
//#################################################################################
//#####################################################################
//实例生成函数定义
#define ExecuteObjectFunction(__classtype,__funtype,__baseclass) \
__baseclass##Facotry::Execute(__classtype,__funtype);
//#####################################################################
//#######################################################################
//对象-对象类型映射,隐藏工厂生成过程
BEGIN_CREATE_CLASS(Device);
ON_CREATING_CLASS(DEVICE1,Device1,Device);
ON_CREATING_CLASS(DEVICE2,Device2,Device);
END_CREATE_CLASS(Device)
//#######################################################################
int main(int argc, char* argv[])
{
//调用Device1实例的print1方法
ExecuteObjectFunction(DEVICE1,FUN1,Device);
//调用Device2实例的print1方法
ExecuteObjectFunction(DEVICE2,FUN1,Device);
//调用Device1实例的print2方法
ExecuteObjectFunction(DEVICE1,FUN2,Device);
//调用Device2实例的print2方法
ExecuteObjectFunction(DEVICE2,FUN2,Device);
return 0;
}
以上程序运行结果:
Device1:print1
Device2:print1
Device1:print2
Device2:print2