//Product.h
#ifndef _PRODUCT_H
#define _PRODUCT_H
class AbstractProductA
{
public:
~AbstractProductA();
protected:
AbstractProductA();
};
class AbstractProductB
{
public:
~AbstractProductB();
protected:
AbstractProductB();
};
class ProdcutA1:AbstractProductA
{
public:
ProdcutA1();
~ProductA1();
};
class ProductA2:AbstractProductA
{
public:
ProductA2();
~ProductA2();
};
class ProductB1:AbstractProductB
{
public:
ProductB1();
~ProductB1();
};
class ProductB2:AbstractProductB
{
public:
ProductB2();
~ProductB2();
};
#endif
//Product.cpp
#include "Product.h"
#include <iostream>
using namespace std;
AbstractFactoryA::AbstractFactoryA()
{}
AbstractFactoryA::~AbstractFactoryA()
{}
AbstractFactoryB::AbstractFactoryB()
{}
AbstractFactoryB::~AbstractFactoryB()
{}
ProdcutA1::ProductA1()
{
cout << "ProductA1.." << endl;
}
ProductA1::~ProductA1()
{}
ProdcutA2::ProductA2()
{
cout << "ProductA2.." << endl;
}
ProductA2::~ProductA2()
{}
ProdcutB1::ProductB1()
{
cout << "ProductB1.." << endl;
}
ProductB1::~ProductB1()
{}
ProdcutB2::ProductB2()
{
cout << "ProductB2.." << endl;
}
ProductB2::~ProductB2()
{}
//AbstractFactory.h
#ifndef _FACTORY_H
#define _FACTORY_H
class AbstractProductA;
class AbstractProductB;
class AbstractFactory
{
public:
virtual ~AbstractFactory();
virtual AbstractProductA * CreateProductA() = 0;
virtual AbstractProductB * CreateProductB() = 0;
protected:
AbstractFactory();
};
class ConcreteFactory1
{
public:
ConcreteFactory1();
~ConcreteFactory1();
AbstractProductA * CreateProductA();
AbstractProductB * CreateProductB();
};
class ConcreteFactory2
{
public:
ConcreteFactory2();
~ConcreteFactory2();
AbstractProductA * CreateProductA();
AbstractProductB * CreateProductB();
};
#endif
//AbstractFactory.cpp
#include "AbstractFactory.h"
#include "Product.h"
#include <iostream>
using namespace std;
class AbstractFactory::AbstractFactory()
{}
class AbstractFactory::~AbstractFactory()
{}
class ConcreteFactory1::ConcreteFactory1()
{
}
class ConcreteFactory1::~ConcreteFactory1()
{}
AbstractProductA * class ConcreteFactory1::CreateProductA()
{
return new ProductA1();
}
AbstractProductB * class ConcreteFactory1::CreateProductB()
{
return new ProductB1();
}
class ConcreteFactory2::ConcreteFactory2()
{
}
class ConcreteFactory2::~ConcreteFactory2()
{}
AbstractProductA * class ConcreteFactory2::CreateProductA()
{
return new ProductA2();
}
AbstractProductB * class ConcreteFactory2::CreateProductB()
{
return new ProductB2();
}
//main.cpp
#include "AbstractFactory.h"
#include <iostream>
using namespace std;
void main(int argc, char * argv[])
{
AbstractFactoryA1 *cf1 = new AbstractFactoryA1();
cf1 -> CreateProductA();
cf1 -> CreateProductB();
AbstractFactoryA2 *cf2 = new AbstractFactoryA2();
cf2 -> CreateProductA();
cf2 -> CreateProductB();
return 0;
}