1. 程式人生 > >CnComm多執行緒串列埠通訊類的封裝實現v0.02

CnComm多執行緒串列埠通訊類的封裝實現v0.02

 CnComm多執行緒串列埠通訊類的封裝實現一文中實現了對於cncomm多執行緒串列埠類的簡單封裝,新版本將串列埠管理類改成了工廠類和單例類,實現了對於動態擴充套件新的串列埠,新的版本符合開閉原則。

1.串列埠管理類

#ifndef _COMM_MANAGE_H_
#define _COMM_MANAGE_H_

class CommWrapper;
//抽象基類
class CommAbsFactory
{
public:
	virtual ~CommAbsFactory();
	virtual CommWrapper* createCommWrapperInstance()=0;
protected:
	CommAbsFactory();
};

//串列埠管理工廠具體類
class CommModuleFactory:public CommAbsFactory
{
public:
	static CommModuleFactory* Instance();

	virtual ~CommModuleFactory();
	CommWrapper* createCommWrapperInstance();
	
protected:
	CommModuleFactory();
	CommModuleFactory(const CommModuleFactory&);
	CommModuleFactory& operator= (const CommModuleFactory&);

private:
	static CommModuleFactory* _commMangeinstance;

};


#endif
#include "stdafx.h"
#include "commWrapper.h"
#include "CommManage.h"

CommAbsFactory::CommAbsFactory()
{

}

CommAbsFactory::~CommAbsFactory()
{

}

CommModuleFactory* CommModuleFactory::_commMangeinstance = 0;


CommModuleFactory::CommModuleFactory()
{
}

CommModuleFactory::~CommModuleFactory()
{

}

CommModuleFactory* CommModuleFactory::Instance()
{
	if(_commMangeinstance == 0)
	{
		_commMangeinstance = new CommModuleFactory();
	}
	return _commMangeinstance;
}

CommWrapper* CommModuleFactory::createCommWrapperInstance()
{
	return new CommWrapper();
}