#include <iostream>
#include <string>
using namespace std;
class Product
{
public:
virtual void ShowInfo() = 0;
};
class ProductA : public Product
{
public:
void ShowInfo()
{
cout << "this is product A." << endl;
}
};
class ProductB : public Product
{
public:
void ShowInfo()
{
cout << "this is product B." << endl;
}
};
class Factory
{
public:
virtual Product* createProduct(string type) = 0;
};
class FactoryA : public Factory
{
Product* createProduct(string type)
{
return new ProductA();
}
};
class FactoryB : public Factory
{
Product* createProduct(string type)
{
return new ProductB();
}
};
int main()
{
Factory *facA = new FactoryA();
Product *pA = facA->createProduct("A");
if(pA != nullptr)
pA->ShowInfo();
Factory *facB = new FactoryB();
Product *pB = facB->createProduct("B");
if(pB != nullptr)
pB->ShowInfo();
}