一、 作用
本模式是用来产生一系列相关或相依赖的对象。
二、 模型图
三、 参与者
1、AbstractFactory:一个,对外提供操作接口。
2、ConcreteFactory:多个,负责具体产品的生成。每一个具体的工厂都可以生成以下定义的一系列抽象新产品。
3、AbstractProduct1:一个。一系列产品中的一类抽象产品。
4、ConcreteProduct1:多个,继承自产品1。
5、AbstractProduct2:一个。一系列产品中的一类抽象产品。
6、ConcreteProduct2:多个,继承自产品2。
7、…….
8、AbstractProductN:一个。一系列产品中的一类抽象产品。
9、ConcreteProductN:多个,继承自产品N。
四、 操作流程
既然是抽象工厂,当然不能直接生成产品了。在实际应用中须要实例化一个具体子工厂,由子工厂来生成这一系列产品。每一个子工厂都必须能生成定义的这一系列产品。
五、 C++实现
有女装厂和男装厂,这两个厂都生产衬衣、裤子。女装厂生产女衬衣、女裤。男装厂生产男衬衣、男裤。
以下是C++代码实现:
#include <iostream>
using namespace std;
class AbstractShirt
{
public:
virtual void Show(void) = 0;
};
class WomanShirt: public AbstractShirt
{
public:
void Show(void)
{
cout<<"I am WomanShirt!"<<endl;
}
};
class ManShirt: public AbstractShirt
{
public:
void Show(void)
{
cout<<"I am ManShirt!"<<endl;
}
};
class AbstractThrousers
{
public:
virtual void Show(void) = 0;
};
class WomanThrousers: public AbstractThrousers
{
public:
void Show(void)
{
cout<<"I am WomanThrousers!"<<endl;
}
};
class ManThrousers: public AbstractThrousers
{
public:
void Show(void)
{
cout<<"I am ManThrousers!"<<endl;
}
};
class ClothingAbstractFactory
{
public://产品生产方法
virtual AbstractShirt *CreateShirt(void){return NULL;}
virtual AbstractThrousers *CreateThrousers(void){return NULL;}
virtual void DestroyShirt(AbstractShirt *pShirt){}
virtual void DestroyThrousers(AbstractThrousers *pThrousers){}
//工厂ID
static const int WOMAN_CLOTH_FACTORY = 1;
static const int MAN_CLOTH_FACTORY = 2;
//提供一个具体的服装厂
static ClothingAbstractFactory *GetClothingFactory(int FactoryID);
//回收服装厂
static void FreeClothingFactory(ClothingAbstractFactory *pFactory);
ClothingAbstractFactory(void){}//不允许用户实例化
virtual ~ClothingAbstractFactory(){}
};
class WomanClothFactory: public ClothingAbstractFactory
{
public:
//产品生产方法
AbstractShirt *CreateShirt(void)
{
return new WomanShirt;
}
AbstractThrousers *CreateThrousers(void)
{
return new WomanThrousers;
}
void DestroyShirt(AbstractShirt *pShirt)
{
delete pShirt;
}
void DestroyThrousers(AbstractThrousers *pThrousers)
{
delete pThrousers;
}
};
class ManClothFactory: public ClothingAbstractFactory
{
public:
//产品生产方法
AbstractShirt *CreateShirt(void)
{
return new ManShirt;
}
AbstractThrousers *CreateThrousers(void)
{
return new ManThrousers;
}
void DestroyShirt(AbstractShirt *pShirt)
{
delete pShirt;
}
void DestroyThrousers(AbstractThrousers *pThrousers)
{
delete pThrousers;
}
};
ClothingAbstractFactory *ClothingAbstractFactory::GetClothingFactory(int FactoryID)
{
ClothingAbstractFactory *pFactory = NULL;
switch (FactoryID)
{
case ClothingAbstractFactory::WOMAN_CLOTH_FACTORY:
pFactory = new WomanClothFactory;
break;
case ClothingAbstractFactory::MAN_CLOTH_FACTORY:
pFactory = new ManClothFactory;
break;
default:
break;
}
return pFactory;
}
void ClothingAbstractFactory::FreeClothingFactory(ClothingAbstractFactory *pFactory)
{
delete pFactory;
}
int main(int argc, char **argv)
{
ClothingAbstractFactory *pFactory;
AbstractShirt *pShirt;
AbstractThrousers *pThrousers;
pFactory = ClothingAbstractFactory::GetClothingFactory(ClothingAbstractFactory::WOMAN_CLOTH_FACTORY);
pShirt = pFactory->CreateShirt();
pThrousers = pFactory->CreateThrousers();
pShirt->Show();
pThrousers->Show();
pFactory->DestroyShirt(pShirt);
pFactory->DestroyThrousers(pThrousers);
ClothingAbstractFactory::FreeClothingFactory(pFactory);
pFactory = ClothingAbstractFactory::GetClothingFactory(ClothingAbstractFactory::MAN_CLOTH_FACTORY);
pShirt = pFactory->CreateShirt();
pThrousers = pFactory->CreateThrousers();
pShirt->Show();
pThrousers->Show();
pFactory->DestroyShirt(pShirt);
pFactory->DestroyThrousers(pThrousers);
ClothingAbstractFactory::FreeClothingFactory(pFactory);
return 0;
}