抽象产品A  -> ProductA.h  & ProductA.cpp :


//
//  ProductA.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__ProductA__
#define __AbstractFactory__ProductA__
#include 
class ProductA {
                                                                                                                                                                                       
public:
                                                                                                                                                                                       
    ProductA();
    virtual ~ProductA();
                                                                                                                                                                                       
    virtual void fun() = 0;
                                                                                                                                                                                       
};
#endif /* defined(__AbstractFactory__ProductA__) */


//
//  ProductA.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "ProductA.h"
ProductA::ProductA()
{
                                                                                                                                                                                    
}
ProductA::~ProductA()
{
                                                                                                                                                                                    
}


具体产品A1 -> ProductA1.h  &  ProductA1.cpp :


//
//  ProductA1.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__ProductA1__
#define __AbstractFactory__ProductA1__
#include 
#include "ProductA.h"
using namespace std;
class ProductA1 : public ProductA {
                                                                                                                                                                  
public:
                                                                                                                                                                  
    ProductA1();
    virtual ~ProductA1();
                                                                                                                                                                  
    virtual void fun();
};
#endif /* defined(__AbstractFactory__ProductA1__) */


//
//  ProductA1.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "ProductA1.h"
ProductA1::ProductA1()
{
                                                                                                                                                               
}
ProductA1::~ProductA1()
{
                                                                                                                                                               
}
void ProductA1::fun()
{
    cout << "ProductA1 -> fun()" << endl;
}


具体产品A2 -> ProductA2.h  &  ProductA2.cpp :


//
//  ProductA2.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__ProductA2__
#define __AbstractFactory__ProductA2__
#include 
#include "ProductA.h"
using namespace std;
class ProductA2 : public ProductA {
                                                                                                                                                     
                                                                                                                                                     
public:
                                                                                                                                                     
    ProductA2();
    virtual ~ProductA2();
                                                                                                                                                     
    virtual void fun();
};
#endif /* defined(__AbstractFactory__ProductA2__) */


//
//  ProductA2.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "ProductA2.h"
ProductA2::ProductA2()
{
                                                                                                                                                  
}
ProductA2::~ProductA2()
{
                                                                                                                                                  
}
void ProductA2::fun()
{
    cout << "ProductA2 -> fun()" << endl;
}


抽象产品B -> ProductB.h  &   ProductB.cpp :


//
//  ProductB.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__ProductB__
#define __AbstractFactory__ProductB__
#include 
class ProductB {
                                                                                                                                     
public:
                                                                                                                                     
    ProductB();
    virtual ~ProductB();
                                                                                                                                     
    virtual void fun() = 0;
};
#endif /* defined(__AbstractFactory__ProductB__) */


//
//  ProductB.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "ProductB.h"
ProductB::ProductB()
{
                                                                                                                                  
}
ProductB::~ProductB()
{
                                                                                                                                  
}


具体产品B1 -> ProductB1.h  &  ProductB1.cpp :


//
//  ProductB1.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__ProductB1__
#define __AbstractFactory__ProductB1__
#include 
#include "ProductB.h"
using namespace std;
class ProductB1 : public ProductB{
                                                                                                                    
public:
                                                                                                                    
    ProductB1();
    virtual ~ProductB1();
                                                                                                                    
    virtual void fun();
};
#endif /* defined(__AbstractFactory__ProductB1__) */


//
//  ProductB1.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "ProductB1.h"
ProductB1::ProductB1()
{
                                                                                                                 
}
ProductB1::~ProductB1()
{
                                                                                                                 
}
void ProductB1::fun()
{
    cout << "ProductB1 -> fun()" << endl;
}


具体产品B2 -> ProductB2.h  &  ProductB2.cpp :


//
//  ProductB2.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__ProductB2__
#define __AbstractFactory__ProductB2__
#include 
#include "ProductB.h"
using namespace std;
class ProductB2 : public ProductB{
                                                                                                 
public:
                                                                                                 
    ProductB2();
    virtual ~ProductB2();
                                                                                                 
    virtual void fun();
};
#endif /* defined(__AbstractFactory__ProductB2__) */


//
//  ProductB2.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "ProductB2.h"
ProductB2::ProductB2()
{
                                                                                              
}
ProductB2::~ProductB2()
{
                                                                                              
}
void ProductB2::fun()
{
    cout << "ProductB2 -> fun()" << endl;
}


抽象工厂 -> Factory.h  &   Factory.cpp :


//
//  Factory.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__Factory__
#define __AbstractFactory__Factory__
#include "ProductA.h"
#include "ProductB.h"
class Factory {
                                                                        
public:
                                                                        
    Factory();
    virtual ~Factory();
                                                                        
    virtual ProductA * getProductA() = 0;
    virtual ProductB * getProductB() = 0;
                                                                        
};
#endif /* defined(__AbstractFactory__Factory__) */


//
//  Factory.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "Factory.h"
Factory::Factory()
{
                                                                     
}
Factory::~Factory()
{
                                                                     
}


具体工厂1 -> Factory1.h  &  Factory1.cpp :


//
//  Factory1.h
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __AbstractFactory__Factory1__
#define __AbstractFactory__Factory1__
#include 
#include "Factory.h"
#include "ProductA1.h"
#include "ProductB1.h"
class Factory1 : public Factory {
                                                   
                                                   
public:
                                                   
    Factory1();
    virtual ~Factory1();
                                                   
    virtual ProductA *getProductA();
    virtual ProductB *getProductB();
                                                   
};
#endif /* defined(__AbstractFactory__Factory1__) */


//
//  Factory1.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "Factory1.h"
Factory1::Factory1()
{
                                                
}
Factory1::~Factory1()
{
                                                
}
ProductA * Factory1::getProductA()
{
    return new ProductA1();
}
ProductB * Factory1::getProductB()
{
    return new ProductB1();
}


具体工厂2 -> Factory2.h  &   Factory2.cpp :


//
//  Factory2.cpp
//  AbstractFactory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "Factory2.h"
Factory2::Factory2()
{
                                  
}
Factory2::~Factory2()
{
                                  
}
ProductA * Factory2::getProductA()
{
    return new ProductA2();
}
ProductB * Factory2::getProductB()
{
    return new ProductB2();
}


测试  ->  main.cpp :


//
//  main.cpp
//  Factory
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include 
#include "Factory.h"
#include "Factory1.h"
#include "Factory2.h"
int main(int argc, const char * argv[])
{
    Factory *factory;
               
    factory = new Factory1();   
    ProductA *productA = factory->getProductA();
    ProductB *productB = factory->getProductB();
    productA->fun();
    productB->fun();
               
    delete factory;
    delete productA;
    delete productB;
               
    factory = new Factory2();
    productA = factory->getProductA();
    productB = factory->getProductB();
    productA->fun();
    productB->fun();
               
    delete factory;
    delete productA;
    delete productB;
               
    return 0;
}


测试结果:


ProductA1 -> fun()

ProductB1 -> fun()

ProductA2 -> fun()

ProductB2 -> fun()