在工程实践中,一个大公司产品的实现需要各部门的互相协同,而产品的开发思路和设计用途(架构)需要架构师编写接口(Interface),C++的纯虚函数可以指定类中函数的形参类型,在开发者明确产品要实现的功能后,利用架构师所提供的形参(接口)实现功能。文章以华为电报接受为例
甲方端(华为本公司架构)
#pragma once
#include"iostream"
using namespace std;
class #pragma once
#include"iostream"
using namespace std;
class CSocketProtocal
{
public:
//客户端初始化,获取handle上下
virtual int cltSocketInit() = 0;
//客户端发报文
virtual int cltSocketSend(unsigned char* buf, int buflen) = 0;
//客户端收报文
virtual int cltSocketRev( unsigned char* buf, int* buflen) = 0;
//客户端释放资源
virtual int cltSocketDestory()=0;
};
#pragma once
class CEncDesProtocol
{
public:
CEncDesProtocol()
{
}
virtual ~CEncDesProtocol()
{
}
virtual int EncData(unsigned char* plain, int plainlen, unsigned char* crypdata, int* cryplen) = 0;
virtual int EecData(unsigned char* plain, int plainlen, unsigned char* crypdata, int* cryplen) = 0;
};
#pragma once
#include"iostream"
using namespace std;
#include"CEncDesProtocol.h"
class HWEncDec :public CEncDesProtocol
{
public:
virtual int EncData(unsigned char* plain, int plainlen, unsigned char* crypdata, int* cryplen) ;
virtual int DecData(unsigned char* cryptdata, int cryptlen, unsigned char* plain, int* plainlen) ;
private:
int a = 0;
};
我们可以看到在华为提供的代码中提供了接口的名称和所需要的形参,这些都是纯虚函数,没用函数实现。
下游厂商甲
#pragma once
#include"iostream"
#include"CSocketProtocal.h"
using namespace std;
class CSckFactoryImp1 :public CSocketProtocal
{
public:
//客户端初始化,获取handle上下
virtual int cltSocketInit();
//客户端发报文
virtual int cltSocketSend(unsigned char* buf, int buflen);
//客户端收报文
virtual int cltSocketRev(unsigned char* buf, int* buflen);
//客户端释放资源
virtual int cltSocketDestory();
private:
unsigned char* p;
int len;
};
上面这串代码中,公司甲继承了华为的接口可以对这些接口进行操作
#include"iostream"
#include"CSckFactoryImp1.h"
using namespace std;
//客户端初始化,获取handle上下
int CSckFactoryImp1::cltSocketInit()
{
p = NULL;
len = 0;
return 0;
}
//客户端发报文
int CSckFactoryImp1::cltSocketSend(unsigned char* buf, int buflen)
{
p = new 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::cltSocketDestory()
{
if (p != NULL)
{
delete p;
p = NULL;
len = 0;
}
return 0;
}
公司甲利用接口将功能编写完成。
将功能打包
#define _CRT_SECURE_NO_WARNINGS
#include"iostream"
#include"CSocketProtocal.h"
#include"CSckFactoryImp1.h"
#include"CSckFactoryImp2.h"
#include"CEncDesProtocol.h"
#include"HWEncDec.h"
using namespace std;
class Main
{
public:
Main()
{
this->sp = NULL;
this->ed = NULL;
}
Main(CSocketProtocal* sp, CEncDesProtocol* ed)
{
this->ed = ed;
this->sp = sp;
}
//留一个接口
void setSp(CSocketProtocal* sp)
{
this->sp = sp;
}
void setEd(CEncDesProtocol *ed)
{
this->ed = ed;
}
public:
int SockSendAndRecEncDec( 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;
}
//ed->EncData(in, inlen, data, &datalen);
if (ret != 0)
{
goto end;
}//发送数据之前给数据加密
ret = this->sp->cltSocketSend(in, inlen);
if (ret != 0)
{
goto end;
}
ret =this-> sp->cltSocketRev(out, &datalen);//收到的是密文
//ed->EecData(data, datalen, out, outlen);
end:
ret = this->sp->cltSocketDestory();
return 0;
}
private:
CSocketProtocal* sp;
CEncDesProtocol* ed;
};
int main()
{
int ret = 0;
unsigned char in[4096];
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char*)in, "jsidchsfhjsn");
inlen = 9;
CEncDesProtocol* ep = NULL;
CSocketProtocal* sp = NULL;
sp = new CSckFactoryImp1;
//ep = new HWEncDec;
Main* M=new Main;
M->setSp(sp);
M->setEd(ep);
ret=M->SockSendAndRecEncDec(in, inlen, out, &outlen);
if (ret != 0)
{
printf("M err");
return ret;
}
delete sp;
delete ep;
delete M;
return ret;
}
这只是一部分的代码实现,实际工程中,还需注意多加注释,打包的主函数只是对功能的演示,真是工程下还需要其他岗位进行用户需求划分来设置任务的选择。