#pragma once

#ifndef ABSTRACTFACTORY_H

#define ABSTRACTFACTORY_H

// 抽象基类AbstractProductA,代表产品A 的抽象
class AbstractProductA
{
public:
AbstractProductA() {}
virtual ~AbstractProductA(){};
};

// 派生类ConcreateProductA1,继承自AbstractProductA,代表产品A 的第一种实现
class ConcreateProductA1 : public AbstractProductA
{
public:
ConcreateProductA1();
virtual ~ConcreateProductA1();
};

// 派生类ConcreateProductA2,继承自AbstractProductA,代表产品A 的第二种实现
class ConcreateProductA2 : public AbstractProductA
{
public:
ConcreateProductA2();
virtual ~ConcreateProductA2();
};

// 抽象基类AbstractProductB,代表产品B 的抽象
class AbstractProductB
{
public:
AbstractProductB() {}
virtual ~AbstractProductB(){};
};

// 派生类ConcreateProductB1,继承自AbstractProductB,代表产品B 的第一种实现
class ConcreateProductB1 : public AbstractProductB
{
public:
ConcreateProductB1();
virtual ~ConcreateProductB1();
};

// 派生类ConcreateProductB2,继承自AbstractProductB,代表产品B 的第二种实现
class ConcreateProductB2 : public AbstractProductB
{
public:
ConcreateProductB2();
virtual ~ConcreateProductB2();
};

// 抽象基类AbstractFactory,工厂的抽象类,生产产品A 和产品B
class AbstractFactory
{
public:
AbstractFactory(){}
virtual ~AbstractFactory(){}
virtual AbstractProductA* CreateProductA() = 0;
virtual AbstractProductB* CreateProductB() = 0;
};

// 派生类ConcreateFactory1,继承自AbstractFactory

// 生产产品A 和产品B 的第一种实现
class ConcreateFactory1 : public AbstractFactory
{
public:
ConcreateFactory1();
virtual ~ConcreateFactory1();
virtual AbstractProductA* CreateProductA();
virtual AbstractProductB* CreateProductB();
};

// 派生类ConcreateFactory2,继承自AbstractFactory

// 生产产品A 和产品B 的第二种实现
class ConcreateFactory2 : public AbstractFactory
{
public:
ConcreateFactory2();
virtual ~ConcreateFactory2();
virtual AbstractProductA* CreateProductA();
virtual AbstractProductB* CreateProductB();
};

#endif


#include "StdAfx.h"
#include "abstractfactory_impl.h"

#include <iostream>

ConcreateProductA1::ConcreateProductA1()
{
std::cout << "construction of ConcreateProductA1\n";
}

ConcreateProductA1::~ConcreateProductA1()
{
std::cout << "destruction of ConcreateProductA1\n";
}

ConcreateProductA2::ConcreateProductA2()
{
std::cout << "construction of ConcreateProductA2\n";
}

ConcreateProductA2::~ConcreateProductA2()
{
std::cout << "destruction of ConcreateProductA2\n";
}

ConcreateProductB1::ConcreateProductB1()
{
std::cout << "construction of ConcreateProductB1\n";
}

ConcreateProductB1::~ConcreateProductB1()
{
std::cout << "destruction of ConcreateProductB1\n";
}

ConcreateProductB2::ConcreateProductB2()
{
std::cout << "construction of ConcreateProductB2\n";
}

ConcreateProductB2::~ConcreateProductB2()
{
std::cout << "destruction of ConcreateProductB2\n";
}

ConcreateFactory1::ConcreateFactory1()
{
std::cout << "construction of ConcreateFactory1\n";
}

ConcreateFactory1::~ConcreateFactory1()
{
std::cout << "destruction of ConcreateFactory1\n";
}

AbstractProductA* ConcreateFactory1::CreateProductA()
{
return new ConcreateProductA1();
}

AbstractProductB* ConcreateFactory1::CreateProductB()
{
return new ConcreateProductB1();
}

ConcreateFactory2::ConcreateFactory2()
{
std::cout << "construction of ConcreateFactory2\n";
}

ConcreateFactory2::~ConcreateFactory2()
{
std::cout << "destruction of ConcreateFactory2\n";
}

AbstractProductA* ConcreateFactory2::CreateProductA()
{
return new ConcreateProductA2();
}

AbstractProductB* ConcreateFactory2::CreateProductB()
{
return new ConcreateProductB2();
}


// AbstractFactory.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "abstractfactory_impl.h"
#include <stdlib.h>

//提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
int _tmain(int argc, _TCHAR* argv[])
{
// 生产产品A 的第一种实现
AbstractFactory *pFactory1 = new ConcreateFactory1;
AbstractProductA *pProductA = pFactory1->CreateProductA();

// 生产产品B 的第二种实现
AbstractFactory *pFactory2 = new ConcreateFactory2;
AbstractProductB *pProductB = pFactory2->CreateProductB();

delete pFactory1;
delete pProductA;
delete pFactory2;
delete pProductB;

system("pause");

return 0;
}