class Product
{
public:
virtual void Show()=0;
};
class ProductA:public Product
{
public:
void Show()
{
cout<<"SHowA"<<endl;
}
};
class ProductB:public Product
{
public:
void Show()
{
cout<<"ShowB"<<endl;
}
};
class Factory
{
public:
virtual Product *CreatInstance()=0;
};
class FactoryA:public Factory
{
public:
Product *CreatInstance()
{
return new ProductA();
}
};
class FactoryB:public Factory
{
public:
Product *CreatInstance()
{
return new ProductB();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Factory *fa = new FactoryA();
Product *pa = fa->CreatInstance();
pa->Show();
Factory *fb = new FactoryB();
Product *pb = fb->CreatInstance();
pb->Show();
delete fa,pa,fb,pb;
return 0;
}
工厂方法
最新推荐文章于 2015-12-23 08:33:08 发布