#include <cstdlib>
#include <iostream>
using namespace std;
class Product
{
public:
virtual ~Product(){cout<<"Product 基类析构"<<endl;}
virtual void Do() = 0;
};
class Product1:public Product
{
public:
virtual ~Product1(){cout<<"Product1 析构"<<endl;}
virtual void Do(){cout<<"I am Product1"<<endl;}
};
class Product2:public Product
{
public:
virtual ~Product2(){cout<<"Product2 析构"<<endl;}
virtual void Do(){cout<<"I am Product2"<<endl;}
};
/
class Factory
{
public:
virtual ~Factory(){cout<<"Factory 基类析构"<<endl;}
virtual Product* GetProduct() = 0;
};
class Concrete1Factory:public Factory
{
public:
virtual ~Concrete1Factory(){}
virtual Product* GetProduct(){return new Product1;}
};
class Concrete2Factory:public Factory
{
public:
virtual ~Concrete2Factory(){}
virtual Product* GetProduct(){return new Product2;}
};
///
void UseProduct(Factory* fac)
{
Product* pro = fac->GetProduct();
pro->Do();
delete pro;
}
int main(int argc, char *argv[])
{
Factory* Fac = new Concrete1Factory;
UseProduct(Fac);
delete Fac;
Fac = new Concrete2Factory;
UseProduct(Fac);
delete Fac;
system("PAUSE");
return EXIT_SUCCESS;
}