Factory_Method

class Product
{
public:
virtual ~Product() {}
virtual void DoSomething()=;
}; class ProductA : public Product
{
public:
ProductA() {}
~ProductA() {} virtual void DoSomething();
}; class ProductB : public Product
{
public:
ProductB() {}
~ProductB() {} virtual void DoSomething();
}; class ProductC : public Product
{
public:
ProductC() {}
~ProductC() {} virtual void DoSomething();
}; void ProductA::DoSomething()
{
cout<<"ProductA::DoSomething"<<endl;
} void ProductB::DoSomething()
{
cout<<"ProductB::DoSomething"<<endl;
} void ProductC::DoSomething()
{
cout<<"ProductC::DoSomething"<<endl;
} class Factory
{
public:
virtual ~Factory() {}
virtual Product* CreateProduct()=;
}; class FactoryA : public Factory
{
public:
FactoryA() {}
virtual ~FactoryA() {}
virtual Product* CreateProduct();
}; class FactoryB : public Factory
{
public:
FactoryB() {}
virtual ~FactoryB() {}
virtual Product* CreateProduct();
}; class FactoryC : public Factory
{
public:
FactoryC() {}
virtual ~FactoryC() {}
virtual Product* CreateProduct();
}; Product* FactoryA::CreateProduct()
{
return new ProductA;
} Product* FactoryB::CreateProduct()
{
return new ProductB;
} Product* FactoryC::CreateProduct()
{
return new ProductC;
} #define DESTROY(ptr) if (ptr) { delete ptr; ptr = NULL; } int main(int argc, char *argv[])
{
Factory* pFactory = NULL;
Product* pProduct = NULL; pFactory = new FactoryA;
pProduct = pFactory->CreateProduct();
pProduct->DoSomething(); DESTROY(pFactory);
DESTROY(pProduct); pFactory = new FactoryB;
pProduct = pFactory->CreateProduct();
pProduct->DoSomething(); DESTROY(pFactory);
DESTROY(pProduct); pFactory = new FactoryC;
pProduct = pFactory->CreateProduct();
pProduct->DoSomething(); DESTROY(pFactory);
DESTROY(pProduct); return ;
}
上一篇:SSISDB2:使用TSQL执行Package


下一篇:Spring MVC 入门基础