//以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码

//Product.h

#pragma once
class AbstractProductA {
public:
    virtual ~AbstractProductA();
protected:
    AbstractProductA();
private:

};

class AbstractProductB {
public:
    virtual ~AbstractProductB();
protected:
    AbstractProductB();
private:
};

class ProductA1 :public AbstractProductA {
public:
    ProductA1();
    virtual ~ProductA1();
private:
protected:

};

class ProductA2 :public AbstractProductA {
public:
    ProductA2();
    virtual ~ProductA2();
protected:
private:
};

class ProductB1 :public AbstractProductB {
public:
    ProductB1();
    virtual ~ProductB1();
protected:
private:

};

class ProductB2 : public AbstractProductB {
public:
    ProductB2();
    virtual ~ProductB2();
protected:
private:

};

//Product.cpp

#include"Product.h"
#include<iostream>

AbstractProductA::AbstractProductA(){}
AbstractProductA::~AbstractProductA(){}
AbstractProductB::AbstractProductB(){}
AbstractProductB::~AbstractProductB(){}
ProductA1::ProductA1() {
    std::cout << "ProductA1" << std::endl;
}
ProductA1::~ProductA1(){}
ProductA2::ProductA2() {
    std::cout << "ProductA2" << std::endl;
}
ProductA2::~ProductA2(){}

ProductB1::ProductB1() {
    std::cout << "ProductB1" << std::endl;
}
ProductB1::~ProductB1(){}
ProductB2::ProductB2() {
    std::cout << "ProductB2" << std::endl;
}
ProductB2::~ProductB2() {

}

//AbstractFactory.h

#pragma once
class AbstractProductA;
class AbstractProductB;
class AbstractFactory {

public:
    virtual ~AbstractFactory();
    virtual AbstractProductA* concreateProductA() = 0;
    virtual AbstractProductB* concreateProductB() = 0;
protected:
    AbstractFactory();
private:
};

class concreateFactory1 :public AbstractFactory {
public:
    concreateFactory1();
    virtual ~concreateFactory1();
    AbstractProductA* concreateProductA();
    AbstractProductB* concreateProductB();
protected:
private:
};

class concreateFactory2 :public AbstractFactory {
public:
    concreateFactory2();
    virtual ~concreateFactory2();
    AbstractProductA* concreateProductA();
    AbstractProductB* concreateProductB();
protected:
private:
};

//AbstractFactory.cpp

#include"Product.h"
#include"AbstractFactory.h"
#include<iostream>

AbstractFactory::~AbstractFactory(){}
AbstractFactory::AbstractFactory(){}

concreateFactory1::concreateFactory1(){}
concreateFactory1::~concreateFactory1(){}
AbstractProductA* concreateFactory1::concreateProductA() {
    return new ProductA1();
}
AbstractProductB* concreateFactory1::concreateProductB() {
    return new ProductB1();
}

concreateFactory2::~concreateFactory2(){}
concreateFactory2::concreateFactory2(){}

AbstractProductA* concreateFactory2::concreateProductA()
{
    return new ProductA2();
}
AbstractProductB* concreateFactory2::concreateProductB()
{
    return new ProductB2();
}

//main.cpp

#include"Product.h"
#include"AbstractFactory.h"
#include<iostream>

int main()
{
    AbstractFactory* fac1 = new concreateFactory1();
    fac1->concreateProductA();
    fac1->concreateProductB();
    AbstractFactory* fac2 = new concreateFactory2();
    fac2->concreateProductA();
    fac2->concreateProductB();
    return 0;
}

#include"Product.h"#include"AbstractFactory.h"#include<iostream>
int main(){AbstractFactory* fac1 = new concreateFactory1();fac1->concreateProductA();fac1->concreateProductB();AbstractFactory* fac2 = new concreateFactory2();fac2->concreateProductA();fac2->concreateProductB();return 0;}

12-17 20:45