c++学习笔记(十四):多态的综合运用——模拟发动报文

本项目总工包括四个部分:

1、抽象接口层

2、抽象接口层的实现类层

3、业务框架层

4、测试函数

1、抽象接口层:

class SocketProtocol
{
public:
	SocketProtocol(void)
	{

	}

	virtual ~SocketProtocol(void)
	{

	}

	//客户端初始化 获取handle上下
	virtual int cltSocketInit( /*out*/) = 0;

	//客户端发报文
	virtual int cltSocketSend( unsigned char *buf /*in*/,  int buflen /*in*/) = 0;

	//客户端收报文
	virtual int cltSocketRev( unsigned char *buf /*in*/, int *buflen /*in out*/) = 0;

	//客户端释放资源
	virtual int cltSocketDestory() =  0;
};

2、抽象接口层的实现类层:


定义文件:

class CMySocketImp :public SocketProtocol
{
public:
	CMySocketImp(void);
	~CMySocketImp(void);
	
	//客户端初始化 获取handle上下
	virtual int cltSocketInit();

	//客户端发报文
	virtual int cltSocketSend( unsigned char *buf /*in*/,  int buflen /*in*/);
	
	//客户端收报文
	virtual int cltSocketRev( unsigned char *buf /*in*/, int *buflen /*in out*/);
	

	//客户端释放资源
	virtual int cltSocketDestory();

private:
	char *pbuf;
	int len;
	
};

实现文件:

CMySocketImp::CMySocketImp(void)
{
	pbuf = NULL;
	len = 0;
}

CMySocketImp::~CMySocketImp(void)
{
	
}


//客户端初始化 获取handle上下
 int CMySocketImp::cltSocketInit()
{
	pbuf = NULL;
	len = 0;
	return 0;
}

//客户端发报文
 int CMySocketImp::cltSocketSend( unsigned char *buf /*in*/,  int buflen /*in*/)
{
	if (buf == NULL || buflen<0 )
	{
		return -1;
	}
	this->pbuf = (char *)malloc(buflen *sizeof(char ));
	if (this->pbuf == NULL)
	{
		return -2;
	}
	memcpy(this->pbuf, buf, buflen);
	this->len = buflen;
	return 0;
}
//客户端收报文
 int CMySocketImp::cltSocketRev( unsigned char *buf /*in*/, int *buflen /*in out*/)
{
	if (buf == NULL ||buflen== NULL)
	{
		return -1;
	}
	memcpy(buf, this->pbuf, this->len);
	*buflen = this->len;
	return 0;
}

//客户端释放资源
 int CMySocketImp::cltSocketDestory()
{
	if (this->pbuf != NULL)
	{
		free(this->pbuf);
		this->pbuf= NULL;
		this->len = 0;
	}
	return 0;
}


3、业务框架层:

class MainOPClass
{
public:
	MainOPClass()
	{
		sp = NULL;
	}
	static int MainOP2(SocketProtocol *sp, unsigned char *buf, int buflen, unsigned char *out, int *outlen)
	{
		int ret = 0;
		ret =  sp->cltSocketInit();
		ret = sp->cltSocketSend(buf, buflen);
		ret = sp->cltSocketRev(out, outlen);
		ret = sp->cltSocketDestory();
		return ret;
	}

	int MainOP1( unsigned char *buf, int buflen, unsigned char *out, int *outlen)
	{
		int ret = 0;

		ret =  sp->cltSocketInit();
		ret = sp->cltSocketSend(buf, buflen);
		ret = sp->cltSocketRev(out, outlen);
		ret = sp->cltSocketDestory();
		return ret;
	}
public:
	SocketProtocol * getSp() const 
	{ 
		return sp;
	}
	void setSp(SocketProtocol * val) 
	{ 
		sp = val; 
	}
protected:
private:
	//在框架类中集成了一个协议。。。。
	SocketProtocol *sp;

};


4、测试函数:

void Test()
{
	int ret = 0;
	char buf[1024] = {0};
	int buflen = 10;

	char out[1024] = {0};
	int outlen = 10;
	 SocketProtocol *sp = NULL;
<span style="white-space:pre">	</span>strcpy(buf, "dddddddddddddssssssssssssssssss");
	sp = new CMySocketImp3();

	MainOPClass mainOpClass;
	mainOpClass.setSp(sp); //注入

	//在MainOPClass中包含了一个协议类sp,这种关系强
	mainOpClass.MainOP1((unsigned char *)buf, buflen, (unsigned char *)out, &outlen);

	//对sp 做函数参数。。。。
	//sp只是做函数参数,关系弱。。。
	mainOpClass.MainOP2(sp, (unsigned char *)buf, buflen, (unsigned char *)out, &outlen);

	MainOPClass::MainOP2(sp, (unsigned char *)buf, buflen, (unsigned char *)out, &outlen);
	printf("out:%s \n", out);
	delete sp;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值