作用:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。
UML结构图:
![](http://www.yesky.com/20020723/fangz20723_6.gif)
抽象基类:
1)Product:创建出来的对象的抽象基类.
2)Factory创建对象的工厂方法的抽象基类.
接口函数:
1)Creator::FactoryMethod:纯虚函数,由派生类实现,创建出对应的Product.
解析:
在这个模式中,有两个抽象基类,一个是Product为创建出来的对象的抽象基类,一个是Factory是工厂的抽象基类,在互相协作的时候都是由相应的Factory派生类来生成Product的派生类,也就是说如果要新增一种Product那么也要对应的新增一个Factory,创建的过程委托给了这个Factory.也就是说一个Factory和一个Product是一一对应的关系.
备注:
设计模式的演示图上把Factory类命名为Creator,下面的实现沿用了这个命名.
演示实现:
1)Factory.h
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/06/30
filename: Factory.h
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Factory模式的演示代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#ifndef FACTORY_H
#define FACTORY_H
![](/Images/OutliningIndicators/None.gif)
class Product
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Product()
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~Product()
{}
};
![](/Images/OutliningIndicators/None.gif)
class ConcreateProduct
: public Product
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
ConcreateProduct();
virtual ~ConcreateProduct();
};
![](/Images/OutliningIndicators/None.gif)
class Creator
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Creator()
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~Creator()
{}
![](/Images/OutliningIndicators/InBlock.gif)
void AnOperation();
![](/Images/OutliningIndicators/InBlock.gif)
protected:
virtual Product* FactoryMethod() = 0;
};
![](/Images/OutliningIndicators/None.gif)
class ConcreateCreator
: public Creator
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
ConcreateCreator();
virtual ~ConcreateCreator();
![](/Images/OutliningIndicators/InBlock.gif)
protected:
virtual Product* FactoryMethod();
};
![](/Images/OutliningIndicators/None.gif)
#endif
![](/Images/OutliningIndicators/None.gif)
2)Factory.cpp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/06/30
filename: Factory.cpp
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Factory模式的演示代码
*********************************************************************/
#include "Factory.h"
#include <iostream>
![](/Images/OutliningIndicators/None.gif)
using namespace std;
![](/Images/OutliningIndicators/None.gif)
ConcreateProduct::ConcreateProduct()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "construction of ConcreateProduct\n";
}
![](/Images/OutliningIndicators/None.gif)
ConcreateProduct::~ConcreateProduct()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "destruction of ConcreateProduct\n";
}
![](/Images/OutliningIndicators/None.gif)
void Creator::AnOperation()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
Product* p = FactoryMethod();
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << "an operation of product\n";
}
![](/Images/OutliningIndicators/None.gif)
ConcreateCreator::ConcreateCreator()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "construction of ConcreateCreator\n";
}
![](/Images/OutliningIndicators/None.gif)
ConcreateCreator::~ConcreateCreator()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "destruction of ConcreateCreator\n";
}
![](/Images/OutliningIndicators/None.gif)
Product* ConcreateCreator::FactoryMethod()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
return new ConcreateProduct();
}
![](/Images/OutliningIndicators/None.gif)
3)Main.cpp(测试代码)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/06/30
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: 测试Factory模式的代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#include "Factory.h"
#include <stdlib.h>
![](/Images/OutliningIndicators/None.gif)
int main(int argc,char* argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
Creator *p = new ConcreateCreator();
p->AnOperation();
![](/Images/OutliningIndicators/InBlock.gif)
delete p;
![](/Images/OutliningIndicators/InBlock.gif)
system("pause");
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)