C++ 工厂模式
定义抽象类
class IParser
{
public:
virtual bool GetStructData(PBYTE bufIn,DWORD bufLength,string cxmlString) = 0;
virtual string CreateXMLData(PBYTE bufIn,DWORD bufLength) = 0;
};
派生类:
class CGetUserInfoParser :public IParser
{
public:
bool GetStructData(PBYTE bufIn,DWORD bufLength,string cxmlString);
string CreateXMLData(PBYTE bufIn,DWORD bufLength);
CGetUserInfoParser();
~CGetUserInfoParser();
};
class CRegisterParser : public IParser
{
public:
virtual bool GetStructData(PBYTE bufIn,DWORD bufLength,string cxmlString);
virtual string CreateXMLData(PBYTE bufIn,DWORD bufLength);
};
抽象工厂:
class ParserFactory
{
public:
static IParser* CreateParser(_EActionType type);
protected:
//constructor/destructoric
};
IParser* ParserFactory::CreateParser(_EActionType type)
{
switch(type)
{
case _EActionType::E_ACTION_getUserInfo:
return new CGetUserInfoParser();
break;
case _EActionType::E_ACTION_handsetAuthenticate:
return new CHandsetAuthenticateParser ();
break;
case _EActionType::E_ACTION_register:
return new CRegisterParser();
break;
default:
return NULL;
}
}
调用:
IParser* poRecord;
poRecord=ParserFactory::CreateParser(_EActionType::E_ACTION_handsetAuthenticate);
poRecord->GetStructData();
delete poRecord;