作用:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。

UML结构图:
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
ExpandedBlockStart.gif ContractedBlock.gif /**/ /********************************************************************
InBlock.gif    created:    2006/06/30
InBlock.gif    filename:     Factory.h
InBlock.gif    author:        李创
InBlock.gif                
http://www.cppblog.com/converse/
InBlock.gif
InBlock.gif    purpose:    Factory模式的演示代码
ExpandedBlockEnd.gif********************************************************************
*/

None.gif
None.gif#ifndef FACTORY_H
None.gif
#define  FACTORY_H
None.gif
None.gif
class  Product
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    Product()
dot.gif{}
ExpandedSubBlockStart.gifContractedSubBlock.gif    
virtual ~Product()dot.gif{}
ExpandedBlockEnd.gif}
;
None.gif
None.gif
class  ConcreateProduct
None.gif    : 
public  Product
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    ConcreateProduct();
InBlock.gif    
virtual ~ConcreateProduct();
ExpandedBlockEnd.gif}
;
None.gif
None.gif
class  Creator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    Creator()
dot.gif{}
ExpandedSubBlockStart.gifContractedSubBlock.gif    
virtual ~Creator()dot.gif{}
InBlock.gif
InBlock.gif    
void AnOperation();
InBlock.gif
InBlock.gif
protected:
InBlock.gif    
virtual Product* FactoryMethod() = 0;
ExpandedBlockEnd.gif}
;
None.gif
None.gif
class  ConcreateCreator
None.gif    : 
public  Creator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    ConcreateCreator();
InBlock.gif    
virtual ~ConcreateCreator();
InBlock.gif
InBlock.gif
protected:
InBlock.gif    
virtual Product* FactoryMethod();
ExpandedBlockEnd.gif}
;
None.gif
None.gif
#endif
None.gif

2)Factory.cpp
ExpandedBlockStart.gif ContractedBlock.gif /**/ /********************************************************************
InBlock.gif    created:    2006/06/30
InBlock.gif    filename:     Factory.cpp
InBlock.gif    author:        李创
InBlock.gif                
http://www.cppblog.com/converse/
InBlock.gif
InBlock.gif    purpose:    Factory模式的演示代码
ExpandedBlockEnd.gif********************************************************************
*/

None.gif#include 
" Factory.h "
None.gif#include 
< iostream >
None.gif
None.gif
using   namespace  std;
None.gif
None.gifConcreateProduct::ConcreateProduct()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    std::cout 
<< "construction of ConcreateProduct\n";
ExpandedBlockEnd.gif}

None.gif
None.gifConcreateProduct::
~ ConcreateProduct()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    std::cout 
<< "destruction of ConcreateProduct\n";
ExpandedBlockEnd.gif}

None.gif
None.gif
void  Creator::AnOperation()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Product
* p = FactoryMethod();
InBlock.gif
InBlock.gif    std::cout 
<< "an operation of product\n";
ExpandedBlockEnd.gif}

None.gif
None.gifConcreateCreator::ConcreateCreator()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    std::cout 
<< "construction of ConcreateCreator\n";
ExpandedBlockEnd.gif}

None.gif
None.gifConcreateCreator::
~ ConcreateCreator()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    std::cout 
<< "destruction of ConcreateCreator\n";
ExpandedBlockEnd.gif}

None.gif
None.gifProduct
*  ConcreateCreator::FactoryMethod()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return new ConcreateProduct();
ExpandedBlockEnd.gif}

None.gif

3)Main.cpp(测试代码)
ExpandedBlockStart.gif ContractedBlock.gif /**/ /********************************************************************
InBlock.gif    created:    2006/06/30
InBlock.gif    filename:     Main.cpp
InBlock.gif    author:        李创
InBlock.gif                
http://www.cppblog.com/converse/
InBlock.gif
InBlock.gif    purpose:    测试Factory模式的代码
ExpandedBlockEnd.gif********************************************************************
*/

None.gif
None.gif#include 
" Factory.h "
None.gif#include 
< stdlib.h >
None.gif
None.gif
int  main( int  argc, char *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Creator 
*= new ConcreateCreator();
InBlock.gif    p
->AnOperation();
InBlock.gif
InBlock.gif    delete p;
InBlock.gif
InBlock.gif    system(
"pause");
InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}