一个精巧的C++类工厂,实现根据类的种类,方法的种类生成实例调用,请您拍砖,多提建议

hatingfarmer 2010-08-10 08:39:39
该代码是我业余时间想到的,目的是根据类的种类,类中方法的种类,生成统一调用接口。
最终目的是可以支持所有的类。下面代码刚写尚未开始优化,请各位贡献您的主意。谢谢。



#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;
}



...全文
387 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hatingfarmer 2010-08-10
  • 打赏
  • 举报
回复
各位请交流
hatingfarmer 2010-08-10
  • 打赏
  • 举报
回复
对于万能工厂的实现,我做了如下优化,各位决定怎么样

#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;
}

hatingfarmer 2010-08-10
  • 打赏
  • 举报
回复
我想如果考虑宏的代码生成功能是可以做到的,不过我觉得是否还有更好的方法,请各位继续支招,讨论就是进步
[Quote=引用 8 楼 babilife 的回复:]
楼主想写个万能接口?

劝楼主不要想了,那是不存在的,你只能针对某一个项目进行最大的优化,万能的东西是不存在的
不过楼主思想值得学习!
[/Quote]
sunlin7 2010-08-10
  • 打赏
  • 举报
回复
还行,呵呵~~我眼拙,没有看出来精巧的地方。
至善者善之敌 2010-08-10
  • 打赏
  • 举报
回复
楼主想写个万能接口?

劝楼主不要想了,那是不存在的,你只能针对某一个项目进行最大的优化,万能的东西是不存在的
不过楼主思想值得学习!
hatingfarmer 2010-08-10
  • 打赏
  • 举报
回复

以上程序运行结果:
Device1:print1
Device2:print1
Device1:print2
Device2:print2
hatingfarmer 2010-08-10
  • 打赏
  • 举报
回复
这是一个我项目代码中抽取的一个思想,主要是为了将不同类中的不同方法调用,用一个接口来实现,
我想肯定还有可以优化的地方,请各位提出宝贵意见
maple_zhj 2010-08-10
  • 打赏
  • 举报
回复
没看出什么特别的地方。
hatingfarmer 2010-08-10
  • 打赏
  • 举报
回复
你好,我声明一下:
我这里的根据类的种类,类中方法的种类,生成统一调用接口,意思是对于不同类,不同方法都采用统一调用接口
[Quote=引用 2 楼 zyq5945 的回复:]
用虚函数就行了吧
[/Quote]
zyq5945 2010-08-10
  • 打赏
  • 举报
回复
用虚函数就行了吧
jyh_baoding 2010-08-10
  • 打赏
  • 举报
回复
思维不错,帮顶
RobyLTE 2010-08-10
  • 打赏
  • 举报
回复
楼主,不知道上面代码是否真是出自楼主之手,如果是,不知道楼主目前从事那行工作,
是否有兴趣考虑工作机会,我们是全球知名的通信设备供应商,为您提供非常好的待遇和空间,不过前提是上述代码真是出自您的
作品。如有兴趣请交流,qq:155170160

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧