定义一个单一创建接口,让子类实现不同对象的创建。
代码示例:
//interface
class Product
{
};
//interface
class Creator
{
public:
virtual Product *createProduct() = 0; //factory method
};
class Product1
{};
class Creator1 : public Creator
{
public:
Product *createProduct()
{
return new Product1;
}
};