{
public:
virtual ~Product() = 0;
protected:
Product();
};
class ConcreteProduct : public Product
{
public:
~ConcreteProduct();
ConcreteProduct();
};
class Factory
{
public:
virtual ~Factory(void) = 0;
virtual Product* CreateProduct() = 0;
protected:
Factory(void);
};
class ConcreteFactory: public Factory
{
public:
~ConcreteFactory();
ConreteFactory();
Product* CreateProduct();
};
/*****************************************************************************/
Product::Product()
{}
Product::~Product()
{}
ConcreteProduct::ConcreteProduct()
{
std::cout << "ConcreteProduct..." << std::endl;
}
ConcreteProduct::~ConcreteProduct()
{
}
Factory::Factory(void)
{
}
Factory::~Factory(void)
{
}
ConcreteFactory::ConreteFactory()
{
std::cout << "ConcreteFactory" << std::endl;
}
ConcreteFactory::~ConcreteFactory()
{}
Product* ConcreteFactory::CreateProduct()
{
return new ConcreteProduct();
}
/***********************/
Factory* fac = new ConcreteFactory();
Product* p = fac->CreateProduct();
Factory模式的两个最重要的功能:
1.定义创建对象的接口,封装了对象的创建
2.使得具体化类的工作推迟到了子类中
我看主要就是1,1使得必须按2的方式来,
并不是基类中不知道要创建哪个类,要推倒子类中才知道如何创建