抽象工厂模式(AbstractFactory)和反射技术(Reflect)都在前面的笔记中记录了,现在将两个功能结合起来。还是以快餐店的食品为例子。
首先,有快餐店的食品作为产品的基类,然后有鸡腿和汉堡两类食品,而鸡腿和汉堡又分肯德基的和麦当劳的。肯德基和麦当劳的汉堡和鸡腿是不同的,也就是说是不同的实现类型,合乎抽象工厂的使用场合。
下面看具体的实现代码:
// ReflectFactory.h
#pragma once
#include <string>
typedef void* (*lpCreateInstanceFun)();
typedef struct tagClassInfoData
{
std::string strClassName;
lpCreateInstanceFun pFun;
tagClassInfoData* pNext;
}ClassInfoData;
class CReflectFactory
{
private:
static ClassInfoData* pData;
static ClassInfoData* pHead;
static CReflectFactory _tmp_;
public:
CReflectFactory(void);
~CReflectFactory(void);
static void AddInstance(const std::string& strClassName, lpCreateInstanceFun pFun);
static void* GetInstance(const std::string& strClassName);
};
#define DECLARE_REFLECT(CLASS_NAME) private:\
static void* _CreateInstance_(){ return new CLASS_NAME(); }\
static wchar_t _m_AddFunc_;\
static wchar_t _AddFunc_() { CReflectFactory::AddInstance(#CLASS_NAME, &_CreateInstance_); return 0; }
#define IMPLEMENT_REFLECT(CLASS_NAME) wchar_t CLASS_NAME::_m_AddFunc_ = _AddFunc_();
// ReflectFactory.cpp
#include ".\ReflectFactory.h"
ClassInfoData* CReflectFactory::pData = NULL;
ClassInfoData* CReflectFactory::pHead = NULL;
CReflectFactory CReflectFactory::_tmp_;
CReflectFactory::CReflectFactory(void)
{
}
CReflectFactory::~CReflectFactory(void)
{
ClassInfoData* pFind = pHead;
while ( pFind != NULL )
{
pFind = pHead->pNext;
delete pHead;
pHead = pFind;
}
}
void CReflectFactory::AddInstance(const std::string& strClassName, lpCreateInstanceFun pFun)
{
if ( NULL != pFun )
{
if ( NULL == pData )
{
pData = new ClassInfoData();
pHead = pData;
}
else
{
pData->pNext = new ClassInfoData();
pData = pData->pNext;
}
pData->strClassName = strClassName;
pData->pFun = pFun;
pData->pNext = NULL;
}
}
void* CReflectFactory::GetInstance(const std::string& strClassName)
{
std::string str = strClassName;
ClassInfoData* pFind = pHead;
while ( pFind != NULL )
{
if ( str == pFind->strClassName )
{
return pFind->pFun(); // 这个括号别再忘了
}
pFind = pFind->pNext;
}
return NULL;
}
//Product.h
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
#include <string>
#include "ReflectFactory.h"
// 快餐店的食品
class AbstractProduct
{
DECLARE_REFLECT(AbstractProduct);
public:
virtual ~AbstractProduct();
protected:
AbstractProduct();
private:
};
// 鸡腿
class AbstractProductA : public AbstractProduct
{
DECLARE_REFLECT(AbstractProductA);
public:
virtual ~AbstractProductA();
protected:
AbstractProductA();
private:
};
// 汉堡
class AbstractProductB : public AbstractProduct
{
DECLARE_REFLECT(AbstractProductB);
public:
virtual ~AbstractProductB();
protected:
AbstractProductB(); private:
};
// 肯德基的鸡腿
class ProductA1:public AbstractProductA
{
DECLARE_REFLECT(ProductA1);
public:
ProductA1();
~ProductA1();
protected: private:
};
// 麦当劳的鸡腿
class ProductA2:public AbstractProductA
{
DECLARE_REFLECT(ProductA2);
public:
ProductA2();
~ProductA2();
protected: private:
};
// 肯德基的汉堡
class ProductB1:public AbstractProductB
{
DECLARE_REFLECT(ProductB1);
public:
ProductB1();
~ProductB1();
protected:
private:
};
// 麦当劳的汉堡
class ProductB2:public AbstractProductB
{
DECLARE_REFLECT(ProductB2);
public:
ProductB2();
~ProductB2();
protected: private:
};
#endif //~_PRODUCT_H_ECT_H_
//Product.cpp
#include "Product.h"
#include <iostream>
using namespace std;
IMPLEMENT_REFLECT(AbstractProduct);
AbstractProduct::AbstractProduct()
{
cout<<"AbstractProduct...快餐店的食品"<<endl;
}
AbstractProduct::~AbstractProduct()
{
}
IMPLEMENT_REFLECT(AbstractProductA);
AbstractProductA::AbstractProductA()
{
cout<<"AbstractProductA...鸡腿"<<endl;
}
AbstractProductA::~AbstractProductA()
{
}
IMPLEMENT_REFLECT(AbstractProductB);
AbstractProductB::AbstractProductB()
{
cout<<"AbstractProductB...汉堡"<<endl;
}
AbstractProductB::~AbstractProductB()
{
}
IMPLEMENT_REFLECT(ProductA1);
ProductA1::ProductA1()
{
cout<<"ProductA1...肯德基的鸡腿"<<endl;
}
ProductA1::~ProductA1()
{
}
IMPLEMENT_REFLECT(ProductA2);
ProductA2::ProductA2()
{
cout<<"ProductA2...麦当劳的鸡腿"<<endl;
}
ProductA2::~ProductA2()
{
}
IMPLEMENT_REFLECT(ProductB1);
ProductB1::ProductB1()
{
cout<<"ProductB1...肯德基的汉堡"<<endl;
}
ProductB1::~ProductB1()
{
}
IMPLEMENT_REFLECT(ProductB2);
ProductB2::ProductB2()
{
cout<<"ProductB2...麦当劳的汉堡"<<endl;
}
ProductB2::~ProductB2()
{
}
// main.cpp
#include "ReflectFactory.h"
#include "Product.h"
int main()
{
AbstractProduct* p0 = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("AbstractProduct"));
AbstractProduct* pA = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("AbstractProductA"));
AbstractProduct* pB = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("AbstractProductB"));
AbstractProduct* pA1 = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("ProductA1"));
AbstractProduct* pA2 = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("ProductA2"));
AbstractProduct* pB1 = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("ProductB1"));
AbstractProduct* pB2 = static_cast<AbstractProduct*>(CReflectFactory::GetInstance("ProductB2"));
return 0;
}
从main函数中的客户端代码就可以看出反射的威力所在了。客户不需要知道除产品名称外的任何信息,也不需要知道具体创建产品的工厂信息,并且在工厂内部代码中,也避免了Factory模式中的对于不同的产品需要用选择或分支语句去实现具体的产品代码,更好的满足了OCP(见《02.工厂模式--Factory》)。