抽象工厂模式
/************************************************************************/
/*设计模式
抽象工厂模式
抽象工厂模式和工厂方法模式非常相似
相同点:一个具体的工厂类对应一个具体的产品类,每个产品类中只有一种具体的产品
不同点:一个具体的工厂类对应若干个具体的产品类,每个产品类分别有若干种归属,
比如本文的进口汽车零件的例子,两种零件【车轮】和【发动机】,
而【轮子】和【发动机】这两个产品类又有不同的归属,
即
【轮子】:【奔驰轮子】、【宝马轮子】
【发动机】:【奔驰发动机】、【宝马发动机】
比如【大话】中提到的:User表和Department表为不同的产品大类,
在逻辑上,大类产品的分类之间是对等的,如前文提到的表【User】和【Department】,
而【User】和【Department】表 又可以在不同的数据库【SQLServer】和【Oracle】中。
/************************************************************************/
Demo
本文以进口汽车配件为例
两个产品:【车轮】 【发动机】
这两个产品有的归属:【奔驰车】 【宝马车】
上代码
【车轮】类、【奔驰车轮】类、【宝马车轮】类
//【车轮类】
class IWheel
{
public:
IWheel();
virtual ~IWheel();
virtual void Import() = 0;
};
IWheel::IWheel(){}
IWheel::~IWheel(){}
//【奔驰车轮类】
class CBenzWheel : public IWheel
{
public:
CBenzWheel();
virtual ~CBenzWheel();
virtual void Import();
};
CBenzWheel::CBenzWheel(){}
CBenzWheel::~CBenzWheel(){}
void CBenzWheel::Import(){cout<<"引进 奔驰品牌 车轮"<<endl;}
//【宝马车轮类】
class CBMWWheel : public IWheel
{
public:
CBMWWheel();
virtual ~CBMWWheel();
virtual void Import();
};
CBMWWheel::CBMWWheel(){}
CBMWWheel::~CBMWWheel(){}
void CBMWWheel::Import(){cout<<"引进 宝马品牌 车轮"<<endl;}
【发动机】类、【奔驰发动机】类、【宝马发动机】类
//【发动机类】
class IEngine
{
public:
IEngine();
virtual ~IEngine();
virtual void Import() = 0;
};
IEngine::IEngine(){}
IEngine::~IEngine(){}
//【奔驰发动机类】
class CBenzEngine : public IEngine
{
public:
CBenzEngine();
virtual ~CBenzEngine();
virtual void Import();
};
CBenzEngine::CBenzEngine(){}
CBenzEngine::~CBenzEngine(){}
void CBenzEngine::Import(){cout<<"引进 奔驰品牌 发动机"<<endl;}
//【宝马发动机类】
class CBMWEngine : public IEngine
{
public:
CBMWEngine();
virtual ~CBMWEngine();
virtual void Import();
};
CBMWEngine::CBMWEngine(){}
CBMWEngine::~CBMWEngine(){}
void CBMWEngine::Import(){cout<<"引进 宝马品牌 发动机"<<endl;}
【抽象工厂】类、 【奔驰工厂】类、【宝马工厂】类
//【抽象工厂类】
class IFactory
{
public:
IFactory();
virtual ~IFactory();
virtual IWheel* CreateWheel() = 0;
virtual IEngine* CreateEngine() = 0;
protected:
IWheel* m_ptrWheel;
IEngine* m_ptrEngine;
};
IFactory::IFactory() : m_ptrEngine(NULL), m_ptrWheel(NULL){}
IFactory::~IFactory()
{
if(NULL != m_ptrWheel) {delete m_ptrWheel; m_ptrWheel = NULL;}
if(NULL != m_ptrEngine){delete m_ptrEngine; m_ptrEngine = NULL;}
}
//【奔驰工厂类】
class CBenzFactory : public IFactory
{
public:
CBenzFactory();
virtual ~CBenzFactory();
virtual IWheel* CreateWheel();
virtual IEngine* CreateEngine();
};
CBenzFactory::CBenzFactory() : IFactory(){}
CBenzFactory::~CBenzFactory(){}
IWheel* CBenzFactory::CreateWheel() {return (m_ptrWheel = new CBenzWheel());}
IEngine* CBenzFactory::CreateEngine() {return (m_ptrEngine = new CBenzEngine());}
//【宝马工厂类】
class CBMWFactory : public IFactory
{
public:
CBMWFactory();
virtual ~CBMWFactory();
virtual IWheel* CreateWheel();
virtual IEngine* CreateEngine();
};
CBMWFactory::CBMWFactory() : IFactory(){}
CBMWFactory::~CBMWFactory(){}
IWheel* CBMWFactory::CreateWheel() { return (m_ptrWheel = new CBMWWheel());}
IEngine* CBMWFactory::CreateEngine() {return (m_ptrEngine = new CBMWEngine());}
客户端
int _tmain(int argc, _TCHAR* argv[])
{
IFactory *pFactory = new CBenzFactory();
IWheel* pWheel = pFactory->CreateWheel();
pWheel->Import();
cout<<endl;
IEngine* pEngine = pFactory->CreateEngine();
pEngine->Import();
cout<<endl;
cout<<endl<<endl;
delete pFactory;
pFactory = new CBMWFactory();
pWheel = pFactory->CreateWheel();
pWheel->Import();
cout<<endl;
pEngine = pFactory->CreateEngine();
pEngine->Import();
cout<<endl<<endl;
delete pFactory;
return 0;
}
运行结果