//
// Factory Method Pattern
// - Define an interface for creating an object, but let subclasses decide
// which class to instantiate. Factory Method lets a class defer
// instantiation to subclasses
//
// Author : ZAsia
// Data : 15/05/06
// Warning : In practice, declaration and implementation should be
// separated(in .h and .cpp).
//
#include
using namespace std;
// Product
// - defines the interface of objects the factory method creates
class Product
{
public:
virtual void Show() = 0;
};
// ConcreteProduct
// - implements the Product interface
class ConcreteProductA : public Product
{
public:
virtual void Show() { cout << "I'm ProductA..." << endl; }
};
class ConcreteProductB : public Product
{
public:
virtual void Show() { cout << "I'm ProductB..." << endl; }
};
// Creator
// - declares the factory method, which returns an object of type Product.
// Creator may also define a default implementation of the factory method
// that returns a default ConcreteProduct object
// - may call the factory method to crete a Product object
class Creator
{
public:
virtual Product *FactoryMethod() = 0;
};
// ConcreteCreator
// - overrides the factory method to return an instance of a ConcreteProduct
class ConcreteCreatorA : public Creator
{
public:
Product *FactoryMethod() { return new ConcreteProductA; }
};
class ConcreteCreatorB : public Creator
{
public:
Product *FactoryMethod() { return new ConcreteProductB; }
};
// Collaborations
// - Creator relies on its subclasses to define the factory method so that
// it returns an instance of the appropriate ConcreteProduct.
int main()
{
Creator *pConcreteCreatorA = new ConcreteCreatorA();
Product *pConcreteProductA = pConcreteCreatorA->FactoryMethod();
pConcreteProductA->Show();
Creator *pConcreteCreatorB = new ConcreteCreatorB();
Product *pConcreteProductB = pConcreteCreatorB->FactoryMethod();
pConcreteProductB->Show();
if (pConcreteCreatorA != NULL)
{
delete pConcreteCreatorA;
pConcreteCreatorA = NULL;
}
if (pConcreteProductA != NULL)
{
delete pConcreteProductA;
pConcreteProductA = NULL;
}
if (pConcreteCreatorB != NULL)
{
delete pConcreteCreatorB;
pConcreteCreatorB = NULL;
}
if (pConcreteProductB != NULL)
{
delete pConcreteProductB;
pConcreteProductB = NULL;
}
return 0;
}
// Implementation
// 1. Two major varieties. The two main variations of the Factory Method pattern
// are (1)the case when the Creator class is an abstract class and does not
// provide an implementation for the factory method it declares, and (2)the case
// when the Creator is a concrete class and provides a default implementation for
// the factory method.
// 2. Parameterized factory methods. Another variation on the pattern lets the factory
// method crate multiple kinds of products. The factory method takes a parameter that
// identifies the kind of object to create. All objects the factory method creates will
// share the Product interface.
//
// A parameterized factory method has the following general form, where MyProduct and
// Your Product are subclasses of Product:
// class Creator
// {
// public:
// virtual Product *Create(ProductId id)
// {
// if (id == MINE) { return new MyProduct; }
// if (id == YOURS) { return new YourProduct; }
// // repeat for remaining products
//
// return 0;
// }
// };
//
// 3. Using templates to avoid subclassing. As we've mentioned, another potential problem
// with factory methods is that they might force you to subclass just to create the
// appropriate Product objets. Another way to get around this in C++ is to provide a
// template subclass of Creator that's parameterized by the Product class.
// class Creator
// {
// public:
// virtual Product *CreateProduct() = 0;
// };
//
// template
// class StandardCreator : public Creator
// {
// public:
// virtual Product *CreateProduct();
// };
//
// template
// Product *StandardCreator
::CreateProduct()
// {
// return new TheProduct;
// }
DesignPatterns_FactoryMethod
最新推荐文章于 2024-05-31 09:41:12 发布