日记...抽象工厂

首次 博客 记录自己的学习历程

有错误之处,还请提出奋斗

代码

#pragma once
#include "string"
#include "memory"
#include "map"

template<class ManufacturedType, typename ClassIDKey = std::string>
class GenericFactory
{
	typedef std::tr1::shared_ptr<ManufacturedType>(*BaseCreateFn)();
	typedef std::map<ClassIDKey, BaseCreateFn> FnRegistry;
	FnRegistry registry;
	GenericFactory(){}
	GenericFactory(const GenericFactory&);
	GenericFactory& operator=(const GenericFactory&);
public:
	static GenericFactory & instance()
	{ 
		static GenericFactory bf; 
		return bf; 
	}
	bool RegCreateFn( const  ClassIDKey & className, BaseCreateFn Fn )
	{
		registry[className] = Fn;
		 return true;
	}
	std::tr1::shared_ptr<ManufacturedType> Create(const ClassIDKey & className  )const
	{
		std::tr1::shared_ptr<ManufacturedType> theObject;
		FnRegistry::const_iterator regEntry = registry.find( className );
		if ( regEntry != registry.end() )
		{
			theObject = regEntry->second();		
		}
		return theObject;
	}
	
};

template<class ManufacturedType, typename ClassIDKey = std::string>
class RegisterInFactory 
{
private:
	static std::tr1::shared_ptr<ManufacturedType> theObj;
	static bool m_bIsSig;
public:
	bool& GetIsSig(){ return m_bIsSig; }
	static std::tr1::shared_ptr<ManufacturedType> CreateInstance()
	{
  		if( (theObj.get() ==NULL && m_bIsSig == true) || m_bIsSig==false )
  		{
			theObj = std::tr1::shared_ptr<ManufacturedType>(new ManufacturedType);
  		}
		else if (theObj.get() != NULL && m_bIsSig == true )
  		{
  		}
		return theObj;
	}

 	 void Register( const ClassIDKey& id  )
 	{
		GenericFactory<ManufacturedType>::instance().RegCreateFn(id,CreateInstance);
 	}

	RegisterInFactory(const ClassIDKey& id, bool m_bSig = false)
	{
		m_bIsSig = m_bSig;
		GenericFactory<ManufacturedType>::instance().RegCreateFn(id,CreateInstance);
	}

	static std::tr1::shared_ptr<ManufacturedType> GetInstance( const ClassIDKey& id )
	{
		return GenericFactory<ManufacturedType>::instance().Create( id );
	}
};

//注册
#define  FACTORY_REGISTRY( classType ) \
	template< >bool RegisterInFactory<classType>::m_bIsSig = false; \
	template< >std::tr1::shared_ptr<classType> RegisterInFactory<classType>::theObj ; \
	RegisterInFactory<classType> classType##123(std::string(#classType)) ;
//注册单列,感觉多余了
#define  FACTORY_REGISTRY_SINGLETON( classType ) \
	template< >bool RegisterInFactory<classType>::m_bIsSig = true; \
	template< >std::tr1::shared_ptr<classType> RegisterInFactory<classType>::theObj; \
	RegisterInFactory<classType> classType##123(std::string(#classType),true) ;
//转成单列的
#define FACTORY_CONTEXT_TO_SINGLETON( classType ) \
	classType##123.GetIsSig() = true;
//同上,反向
#define FACTORY_CONTEXT_TO_NORMAL( classType ) \
	classType##123.GetIsSig() = false;
//得到对象的指针
#define  FACTORY_GET(classType) \
	RegisterInFactory<classType>::GetInstance(std::string(#classType)).get()
//专程对应的指针类型
#define FACTORY_GET_DYNAMIC(classType,classToType) \
	(std::tr1::dynamic_pointer_cast<classToType>(RegisterInFactory<classType>::GetInstance(std::string(#classType)))).get()



使用:

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "GenericFactory.hpp"


class Animal
{
public:
	virtual void PrintType()
	{
		std::cout<<"Animal"<<std::endl;
	}
};
class Bird : public Animal
{
public:
	virtual void PrintType()
	{
		std::cout<<"Bird"<<std::endl;
	}
};

FACTORY_REGISTRY( Animal )
FACTORY_REGISTRY_SINGLETON(Bird)

 int _tmain(int argc, _TCHAR* argv[])
{
	Animal *pAnimal = new Animal;
	Animal *pAnimal2 = FACTORY_GET(Animal);
	FACTORY_CONTEXT_TO_SINGLETON(Animal)
	Animal *pAnimal3 = FACTORY_GET(Animal);

	if ( pAnimal2 == pAnimal3 )
	{
		std::cout<<"Same Point"<<std::endl;
	}
	Animal *pBird2Animal = FACTORY_GET_DYNAMIC( Bird,Animal );

	pAnimal->PrintType();	
	pAnimal2->PrintType();
	pBird2Animal->PrintType();	
	_getch();
	return 0;
}

输出:

Same Point
Animal
Animal
Bird

刚开始接触设计模式,有错误,请提出下,多谢。

注:参考 <<C++对话系列>>



 
 


 
 


 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值