本文主要介绍C++面向对象编程的一个综合示例:socket库C++模型设计和实现--企业信息系统框架集成第三方产品案例。下面是示例的主要内容介绍。
内容介绍:
案例背景:一般的企业信息系统都有成熟的框架,软件框架一般不发生变化,能自由的集成第三方厂商的产品
案例需求:在企业信息系统框架中集成第三方厂商的Socket通信产品和第三方加密产品
第三方厂商的socket通信产品:完成两点之间的通信
第三方厂商加密产品:完成数据发送时加密;数据接收解密
案例要求:1 能支持多个第三方厂商加密产品的入围
2 能支持多个厂商的socket通信产品的入围
3 企业信息系统框架不轻易发生框架
需求实现:思考1:企业信息系统框架,第三方产品如何分层 --> 通过一个接口层进行分层
思考2 :企业信息系统框架如何自由继承第三方产品(软件设计:模块要求松,接口要求紧)--》
思考3 :软件分层以后,开发企业信息系统框架的程序员应该做什么?第三方产品入围应该做什么
编码实现:分析有多少个类:CSocketProtocol,CSckFactoryImp1,CSckFactoryImp2
CEncDesProtocol(加密厂商) HwEncDes CiscoEncDes
1 定义CSocketProtocol 抽象类
2 编写框架函数
3 编写框架测试函数
4 厂商1CSckFactoryImp1实现CSocketProtocol,厂商2实现CSocketProtocol
5 抽象加密接口,加密厂商1,加密厂商2,集成实现业务模型
6 框架(c语言函数方式,框架函数:c++类方式,框架类)
其中主要用到了几个重要的面向对象思想:继承-组合(强弱),注入,控制反转IOC,面向对象思想扩展aop思想(aop思想是对继承编程思想的有力补充)。
下面是具体的代码实现。
一 首先是.h文件:
1.des.h文件
#pragma once
class des
{
public:
//用户使用的函数 加密
int DesEnc(unsigned char *pInData,
int nInDataLen,
unsigned char *poutData,
int *poutDataLen);
// 用户使用函数 des 解密
int DesDec(unsigned char *pInData,
int nInDataLen,
unsigned char *poutData,
int *poutDataLen);
};
2.CEncDesProtocol.h文件
#pragma once
// 加密 解密
class CEncDesProtocol
{
public:
CEncDesProtocol()
{
}
virtual~CEncDesProtocol()
{
}
// 加密 明文--》密文
virtual int EncData(unsigned char *plain,int plainlen,unsigned char *cryptdata,int *cryptdatalen) = 0;
// 解密 密文-->>明文
virtual int DecData(unsigned char *cryptdata,int cryptdatalen,unsigned char *plain,int *plainlen) = 0;
};
3.HwEncDes.h文件
#pragma once
#include "CEncDesProtocol.h"
#include "iostream"
#include "des.h"
using namespace std;
class HwEncDes : public CEncDesProtocol,public des
{
public:
// 加密 明文--》密文
virtual int EncData(unsigned char *plain,int plainlen,unsigned char *cryptdata,int *cryptdatalen);
// 解密 密文-->>明文
virtual int DecData(unsigned char *cryptdata,int cryptdatalen,unsigned char *plain,int *plainlen);
};
4.CSocketProtocol.h文件
#pragma once
#include "iostream"
using namespace std;
class CSocketProtocol
{
public:
CSocketProtocol()
{
;
}
virtual ~CSocketProtocol()//虚析构函数
{
;
}
// 客户端初始化
virtual int cltSocketInit() = 0;
// 客户端发报文
virtual int cltSocketSend(unsigned char *buf,int buflen) = 0;
// 客户端收报文
virtual int cltSocketRev(unsigned char *buf,int *buflen) = 0;
// 客户端释放资源
virtual int cltSocketDestroy() = 0;
protected:
private:
};
5.CSckFactoryImp1.h文件
#pragma once
#include "iostream"
using namespace std;
#include "CSocketProtocol.h"
class CSckFactoryImp1 : public CSocketProtocol
{
public:
// 客户端初始化
virtual int cltSocketInit();
// 客户端发报文
virtual int cltSocketSend(unsigned char *buf,int buflen);
// 客户端收报文
virtual int cltSocketRev(unsigned char *buf,int *buflen);
// 客户端释放资源
virtual int cltSocketDestroy();
protected:
private:
unsigned char *p;
int len;
};
6.CSckFactoryImp2.h文件
#pragma once
#include "iostream"
using namespace std;
#include "CSocketProtocol.h"
class CSckFactoryImp2 : public CSocketProtocol
{
public:
// 客户端初始化
virtual int cltSocketInit();
// 客户端发报文
virtual int cltSocketSend(unsigned char *buf,int buflen);
// 客户端收报文
virtual int cltSocketRev(unsigned char *buf,int *buflen);
// 客户端释放资源
virtual int cltSocketDestroy();
protected:
private:
unsigned char *p;
int len;
};
二 .cpp文件:
1.des.cpp文件
#include "des.h"
//用户使用的函数 加密
int des::DesEnc(unsigned char *pInData,int nInDataLen,unsigned char *poutData,int*poutDataLen)
{
return 0;
}
// 用户使用函数 des 解密
int des::DesDec(unsigned char *pInData,int nInDataLen,unsigned char *poutData,int*poutDataLen)
{
return 0;
}
2.HwEncDes.cpp文件
#include "HwEncDes.h"
#include "des.h"
// 加密 明文--》密文
int HwEncDes::EncData(unsigned char *plain,int plainlen,unsigned char *cryptdata,int *cryptdatalen)
{
int ret = 0;
//用户使用的函数 加密
ret = des::DesEnc(plain,plainlen,cryptdata,cryptdatalen);
if (ret != 0)
{
printf("func DesEnc() err:%d\n",ret);
}
return ret;
}
// 解密 密文-->>明文
int HwEncDes::DecData(unsigned char *cryptdata,int cryptdatalen,unsigned char *plain,int *plainlen)
{
int ret = 0;
// 用户使用函数 des 解密
ret = des::DesDec(cryptdata,cryptdatalen,plain,plainlen);
if (ret != 0)
{
printf("func DesDec() err:%d\n",ret);
}
return ret;
}
3.CSckFactoryImp1.cpp文件
#include "CSckFactoryImp1.h"
#include "iostream"
using namespace std;
// 客户端初始化
int CSckFactoryImp1::cltSocketInit()
{
p = NULL;
len = 0;
return 0;
}
// 客户端发报文
int CSckFactoryImp1::cltSocketSend(unsigned char *buf,int buflen)
{
p = (unsigned char *)malloc(sizeof(unsigned char) * buflen);
if ( p == NULL)
{
return -1;
}
memcpy(p,buf,buflen);
len = buflen;
return 0;
}
// 客户端收报文
int CSckFactoryImp1::cltSocketRev(unsigned char *buf,int *buflen)
{
if (buf == NULL || buflen == NULL)
{
return -1;
}
*buflen = this->len;
memcpy(buf,this->p,this->len);
return 0;
}
// 客户端释放资源
int CSckFactoryImp1::cltSocketDestroy()
{
if ( p != NULL)
{
free(p);
p = NULL;
len = 0;
}
return 0;
}
4.CSckFactoryImp2.cpp文件
#include "CSckFactoryImp2.h"
// 客户端初始化
int CSckFactoryImp2::cltSocketInit()
{
p = NULL;
len = 0;
return 0;
}
// 客户端发报文
int CSckFactoryImp2::cltSocketSend(unsigned char *buf,int buflen)
{
p = (unsigned char *)malloc(sizeof(unsigned char) * buflen);
if ( p == NULL)
{
return -1;
}
memcpy(p,buf,buflen);
len = buflen;
return 0;
}
// 客户端收报文
int CSckFactoryImp2::cltSocketRev(unsigned char *buf,int *buflen)
{
if (buf == NULL || buflen == NULL)
{
return -1;
}
*buflen = this->len;
memcpy(buf,this->p,this->len);
return 0;
}
// 客户端释放资源
int CSckFactoryImp2::cltSocketDestroy()
{
if ( p != NULL)
{
free(p);
p = NULL;
len = 0;
}
return 0;
}
三 main()函数测试示例:
1.main01():
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
//
int SckSendAndRec01(CSocketProtocol *sp,unsigned char *in,int inlen,unsigned char *out,int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketSend(in,inlen);
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketRev(out,outlen);
if ( ret != 0)
{
goto End;
}
End:
ret = sp->cltSocketDestroy();
return 0;
}
//写一个框架
int main01()
{
int ret = 0;
unsigned char in[4096];
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char *)in,"aaaaaabbbbbbbdddd");
inlen = 9;
// 第一个厂商
CSocketProtocol *sp = NULL;
sp = new CSckFactoryImp1;
ret = SckSendAndRec01(sp,in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRec() err:" << ret << endl;
return ret;
}
delete sp;//想通过父类指针,释放所有的子类对象的资源,应使用虚析构函数
// 第二个厂商
CSckFactoryImp2 *sp2 = NULL;
sp2 = new CSckFactoryImp2;
ret = SckSendAndRec01(sp2,in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRec() err:" << ret << endl;
return ret;
}
cout << "hello..."<< endl;
system("pause");
}
2.main02():
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
#include "CEncDesProtocol.h"
#include "des.h"
#include "HwEncDes.h"
//
int SckSendAndRec2(CSocketProtocol *sp,unsigned char *in,int inlen,unsigned char *out,int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketSend(in,inlen);
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketRev(out,outlen);
if ( ret != 0)
{
goto End;
}
End:
ret = sp->cltSocketDestroy();
return 0;
}
//该框架式一个函数 要改为类的形式 见mainclass03_框架类.cpp
int SckSendAndRecDecEnc2(CSocketProtocol *sp,CEncDesProtocol *ed,unsigned char *in,int inlen,unsigned char *out,int *outlen)
{
int ret = 0;
unsigned char data[4096];
int datalen = 0;
ret = sp->cltSocketInit();
if ( ret != 0)
{
goto End;
}
ret = ed->EncData(in,inlen,data,&datalen);// 数据发送之前要加密,发送的是密文
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketSend(data,datalen);// 发送密文
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketRev(data,&datalen);// 收到的是密文,要进行解密
if ( ret != 0)
{
goto End;
}
ret = ed->DecData(data,datalen,out,outlen);//解密
if ( ret != 0)
{
goto End;
}
End:
ret = sp->cltSocketDestroy();
return 0;
}
//写一个框架
int main02()
{
int ret = 0;
unsigned char in[4096];
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char *)in,"aaaaaabbbbbbbdddd");
inlen = 9;
// 第一个厂商
CSocketProtocol *sp = NULL;
sp = new CSckFactoryImp1;
ret = SckSendAndRec2(sp,in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRec() err:" << ret << endl;
return ret;
}
delete sp;//想通过父类指针,释放所有的子类对象的资源,应使用虚析构函数
// 第二个厂商
CSckFactoryImp2 *sp2 = NULL;
sp2 = new CSckFactoryImp2;
ret = SckSendAndRec2(sp2,in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRec() err:" << ret << endl;
return ret;
}
// 加解密
CEncDesProtocol *ed = NULL;
ed = new HwEncDes;
ret = SckSendAndRecDecEnc2(sp,ed,in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRecDecEnc() err:" << ret << endl;
return ret;
}
delete ed;
cout << "hello..."<< endl;
system("pause");
}
3.main03():
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
#include "CEncDesProtocol.h"
#include "des.h"
#include "HwEncDes.h"
// 抽象类在多继承中的应用
//class MainOp : public CSocketProtocol,public CEncDesProtocol
//{
//public:
//
//protected:
//
//private:
//
//};
class MainOp
{
public:
MainOp()
{
this->ed = NULL;
this->sp = NULL;
}
MainOp(CSocketProtocol *sp,CEncDesProtocol *ed)
{
this->ed = ed;
this->sp = sp;
}
void setSp(CSocketProtocol *sp)
{
this->sp = sp;
}
void setEd(CEncDesProtocol *ed)
{
this->ed = ed;
}
public:
int SckSendAndRec(unsigned char *in,int inlen,unsigned char *out,int *outlen)
{
int ret = 0;
ret = this->sp->cltSocketInit();
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketSend(in,inlen);
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketRev(out,outlen);
if ( ret != 0)
{
goto End;
}
End:
ret = this->sp->cltSocketDestroy();
return 0;
}
//该框架式一个函数 改为类的形式
int SckSendAndRecDecEnc(unsigned char *in,int inlen,unsigned char *out,int *outlen)
{
int ret = 0;
unsigned char data[4096];
int datalen = 0;
ret = this->sp->cltSocketInit();
if ( ret != 0)
{
goto End;
}
ret = this->ed->EncData(in,inlen,data,&datalen);// 数据发送之前要加密,发送的是密文
if ( ret != 0)
{
goto End;
}
ret = this->sp->cltSocketSend(data,datalen);// 发送密文
if ( ret != 0)
{
goto End;
}
ret = sp->cltSocketRev(data,&datalen);// 收到的是密文,要进行解密
if ( ret != 0)
{
goto End;
}
ret = ed->DecData(data,datalen,out,outlen);//解密
if ( ret != 0)
{
goto End;
}
End:
ret = sp->cltSocketDestroy();
return 0;
}
protected:
private:
CSocketProtocol *sp;
CEncDesProtocol *ed;
};
//写一个框架
int main()
{
int ret = 0;
unsigned char in[4096];
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char *)in,"aaaaaabbbbbbbdddd");
inlen = 9;
CSocketProtocol *sp = NULL;
CSocketProtocol *sp2 = NULL;
sp = new CSckFactoryImp1;
sp2 = new CSckFactoryImp2;
CEncDesProtocol *ed = NULL;
ed = new HwEncDes;
MainOp *myMainOp = new MainOp;//此时调用无参构造函数MainOp(),需要设置,如下
myMainOp->setSp(sp);
myMainOp->setEd(ed);
// 第二个厂商
myMainOp->SckSendAndRec(in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRec() err:" << ret << endl;
return ret;
}
//加解密
ret = myMainOp->SckSendAndRecDecEnc(in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRecDecEnc() err:" << ret << endl;
return ret;
}
MainOp *m2 = new MainOp(sp,ed);//此时调用有参构造函数,MainOp(CSocketProtocol *sp,CEncDesProtocol *ed)不需要设置
m2->SckSendAndRecDecEnc(in,inlen,out,&outlen);
if ( ret != 0)
{
cout << "func SckSendAndRecDecEnc() err:" << ret << endl;
return ret;
}
delete sp;
delete ed;
delete myMainOp;
delete sp2;
delete m2;
cout << "hello..."<< endl;
system("pause");
}
下面链接是上面的的所有代码,不想复制的同学可以直接下载,只需两个积分:socket库C++模型设计和实现--企业信息系统框架集成第三方产品案例。