DesignPatterns_FactoryMethod

//
// 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;
//	}

      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值