一个工厂负责一个产品
编程实例:
// this is a example for test factory method
#include
using namespace std;
//abstract food
class baseFish
{
public:
// virtual ~baseFish(){};
virtual void getFood(){};
};
class steamedFish:public baseFish
{
virtual void getFood()
{
cout << "this steamed fish" <
};
};
class roastFish:public baseFish
{
virtual void getFood()
{
cout << "this roast fish" <
};
};
//abstract factory
class baseFactory
{
public:
// virtual ~baseFactory(){}
virtual baseFish* createFood(){};
};
class steamedFishFactory:public baseFactory
{
public:
baseFish * createFood()
{
return new steamedFish();
};
};
class roastFishFactory:public baseFactory
{
public:
baseFish * createFood()
{
return new roastFish;
};
};
int main()
{
baseFactory *factory = new steamedFishFactory;
baseFish *fish = factory->createFood();
fish->getFood();
return 0;
}
抽象类不可以定义对象,可以定义指针
阅读(634) | 评论(0) | 转发(0) |