现在的位置: 首页 > 综合 > 正文

Factory.h

2013年09月09日 ⁄ 综合 ⁄ 共 3218字 ⁄ 字号 评论关闭
/*
功能,测试 Factory
日期,2012年11月7日 星期三
博客,http://blog.csdn.net/shunqiziranhao007/article/details/8180116
环境,win7-32-vs2010
*/
#include "Factory.h"
#include <iostream>

using namespace Loki;
using namespace std;

int g_iCount = 0;

int* CreateInt1()
{
	cout << "对象1" << endl;
	return new int(1);
}

int* CreateInt2()
{
	cout << "对象2" << endl;
	return new int(2);
}

class A
{
public:
	virtual void print() = 0;
};

class A1 : public A
{
public:
	void print()
	{
		cout << "A1" << endl;
	}
};

class A2 : public A
{
public:
	void print()
	{
		cout << "A2" << endl;
	}
};

A* CreateA1()
{
	return new A1();
}

A* CreateA2()
{
	return new A2();
}

int main()
{
	// 测试内置类型的

	Factory<int, int> fInt;
	
	fInt.Register(1, CreateInt1);
	fInt.Register(2, CreateInt2);
	
	int *pInt = fInt.CreateObject(1);
	cout << *pInt << endl;
	delete pInt;
	
	pInt = fInt.CreateObject(2);
	cout << *pInt << endl;
	delete pInt;
	
	cout << endl;

	// 测试类的

	Factory<A, int> fA;
	
	fA.Register(1, CreateA1);
	fA.Register(2, CreateA2);
	
	A *pA = fA.CreateObject(1);
	pA->print();
	delete pA;

	pA = fA.CreateObject(2);
	pA->print();
	delete pA;

	return 0;
}
/*
输出效果:

对象1
1
对象2
2

A1
A2

*/

/*
功能,工厂
日期,2012年11月7日 星期三
博客,http://blog.csdn.net/shunqiziranhao007/article/details/8180116
环境,win7-32-vs2010
*/
#ifndef FACTORY_INC_
#define FACTORY_INC_

#include "LokiTypeInfo.h"
#include "AssocVector.h"
#include <exception>

namespace Loki
{
	// Manages the "Unknown Type" error in an object factory
	template <typename IdentifierType, class AbstractProduct>
	struct DefaultFactoryError
	{
		struct Exception : public std::exception
		{
			const char* what() const throw() { return "Unknown Type"; }
		};

		// 没找到合适的生产器,抛出异常
		static AbstractProduct* OnUnknownType(IdentifierType)
		{
			throw Exception();
		}
	};


	// Implements a generic object factory
	// 模板参数,参1,抽象产品类型,参2,类型标志,参3,产品生产器,参4,错误处理
	template
	<
		class AbstractProduct, 
		typename IdentifierType,
		typename ProductCreator = AbstractProduct* (*)(),
		template<typename, class>
		class FactoryErrorPolicy = DefaultFactoryError
	>
	class Factory 
		: public FactoryErrorPolicy<IdentifierType, AbstractProduct>
	{
	public:
		// 注册,类型标志和生产器
		bool Register(const IdentifierType& id, ProductCreator creator)
		{
			return associations_.insert(
				typename IdToProductMap::value_type(id, creator)).second;
		}

		bool Unregister(const IdentifierType& id)
		{
			return associations_.erase(id) == 1;
		}

		// 根据类型标志来生产对象
		AbstractProduct* CreateObject(const IdentifierType& id)
		{
			typename IdToProductMap::iterator i = associations_.find(id);
			if (i != associations_.end())
			{
				return (i->second)();
			}
			return this->OnUnknownType(id);
		}

	private:
		// 关联向量,类似map,键值对
		typedef AssocVector<IdentifierType, ProductCreator> IdToProductMap;
		IdToProductMap associations_;
	};


	// Implements a generic cloning factory
	// 模板参数,参1,抽象产品类型,参2,产品生产器,参数是产品,参3,错误处理
	// 克隆工厂
	template
	<
		class AbstractProduct, 
		class ProductCreator = 
		AbstractProduct* (*)(const AbstractProduct*),
		template<typename, class>
		class FactoryErrorPolicy = DefaultFactoryError
	>
	class CloneFactory
		: public FactoryErrorPolicy<TypeInfo, AbstractProduct>
	{
	public:
		bool Register(const TypeInfo& ti, ProductCreator creator)
		{
			return associations_.insert(
				typename IdToProductMap::value_type(ti, creator)).second;
		}

		bool Unregister(const TypeInfo& id)
		{
			return associations_.erase(id) == 1;
		}

		// 按照model进行生产
		AbstractProduct* CreateObject(const AbstractProduct* model)
		{
			if (model == 0) return 0;

			typename IdToProductMap::iterator i = 
				associations_.find(typeid(*model));
			if (i != associations_.end())
			{
				return (i->second)(model);
			}
			return this->OnUnknownType(typeid(*model));
		}

	private:
		typedef AssocVector<TypeInfo, ProductCreator> IdToProductMap;
		IdToProductMap associations_;
	};
} // namespace Loki

#endif // FACTORY_INC_

抱歉!评论已关闭.