64,680
社区成员
发帖
与我相关
我的任务
分享
#ifndef _Factory_H_
#define _Factory_H_
#include <iostream>
#include "Product.cpp"
using namespace std;
//对不同的产品产生不同的工厂(本身就是一个单例)
template<class T>
class Factory
{
public:
//获取作为成员变量的静态单例的函数
static inline Factory* getinstance()
{
if(!_instance)
_instance = new Factory<T>;
return _instance;
}
T* create()
{
return new T;
}
private:
//作为私有成员的构造函数
Factory()
{
}
//作为私有成员的析构函数
~Factory()
{
}
//作为成员变量的静态单例
static Factory* _instance;
};
//注意::可以使用模板为每个模板类的工厂的静态指针_instance初始化
template<class _T> Factory<_T>* Factory<_T>::_instance = NULL;
#endif
#ifndef __Singleton_H__
#define __Singleton_H__
//T是任何继承此单例的类名
template <class T>
class Singleton
{
public:
//获取类的单例
static inline T* getinstance();
//释放类的单例
void release();
protected:
//作为保护成员的构造函数
Singleton(void){}
//作为保护成员的析构函数
~Singleton(void){}
//作为成员变量的静态单例
static T* _instance;
};
//获取类的单例
template <class T>
inline T* Singleton<T>::getinstance()
{
if(!_instance)
_instance=new T;
return _instance;
}
//释放类的单例
template <class T>
void Singleton<T>::release()
{
if(!_instance)
return;
delete _instance;
_instance=0;
}
//定义一个宏帮助继承此单例模板类的子类的静态指针_instance初始化
#define DECLARE_SINGLETON_MEMBER(_Ty)\
_Ty* Singleton<_Ty>::_instance=NULL
#endif//__Singleton_H__
#ifndef __FactoryNew_H__
#define __FactoryNew_H__
#include "Singleton.h"
//对不同的产品产生不同的工厂类
template <class Pro>
class FactoryNew:public Singleton<FactoryNew<Pro>> //继承自单例类Singleton的工厂模式的模板
{
friend class Singleton<FactoryNew<Pro>>;
public:
Pro *create();
private:
//作为私有成员的构造函数
FactoryNew();
//作为私有成员的析构函数
~FactoryNew();
protected:
};
#endif
#include "FactoryNew.h"
template <class Pro>
//构造函数的定义
FactoryNew<Pro>::FactoryNew()
{
}
//析构函数的定义
template <class Pro>
FactoryNew<Pro>::~FactoryNew()
{
}
template <class Pro>
Pro* FactoryNew<Pro>::create()
{
return new Pro;
}
//问题在此::为什么不能用如下方法一次性给每个模板类的工厂的静态指针初始化
/*
** #define DECLARE_FACTORY_OF(_Pro)\
** FactoryNew<_Pro>* Singleton<FactoryNew<_Pro>>::_instance=NULL
** template<class _Pro> DECLARE_FACTORY_OF(_Pro);
*/
#ifndef _Product_H_
#define _Product_H_
#include <iostream>
using namespace std;
//产品A,由工厂Factory生产
class ProductA
{
public:
ProductA(){}
~ProductA(){}
void put(){cout<<"productA is here"<<endl;}
};
//产品B,由工厂Factory生产
class ProductB
{
public:
ProductB(){}
~ProductB(){}
void put(){cout<<"ProductB is here"<<endl;}
};
//产品C,由工厂FactoryNew生产
class ProductC
{
public:
ProductC(){}
~ProductC(){}
void put(){cout<<"ProductC is here"<<endl;}
};
//产品D,由工厂FactoryNew生产
class ProductD
{
public:
ProductD(){}
~ProductD(){}
void put(){cout<<"productD is here"<<endl;}
};
#endif
#include <iostream>
#include "Factory.cpp"
#include "Singleton.h"
#include "FactoryNew.cpp"
using namespace std;
//必须分别对每个模板类的工厂的静态指针初始化
DECLARE_SINGLETON_MEMBER(FactoryNew<ProductC>);
DECLARE_SINGLETON_MEMBER(FactoryNew<ProductD>);
int main()
{
/////////////////////////////////////////////////////////////////////////////////
//本身就是单例的工厂模式模板Factory
ProductA *proA = Factory<ProductA>::getinstance()->create();
proA->put();
ProductB *proB = Factory<ProductB>::getinstance()->create();
proB->put();
/////////////////////////////////////////////////////////////////////////////////
//继承自单例类Singleton的工厂模式模板FactoryNew
ProductC *proC = FactoryNew<ProductC>::getinstance()->create();
proC->put();
ProductD *proD = FactoryNew<ProductD>::getinstance()->create();
proD->put();
return 0;
}
template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);
就没有报错了。
然后,我还有疑问,为什么将.h和.cpp文件合并后就不会出错?[/quote]
模板的定义一般都放在头文件中的,便于模板实例化。 [/quote]
之前我的main.h文件是#include "FactoryNew.cpp"的,而FactoryNew.cpp中有#include "FactoryNew.h",按道理应该和把factorynew放在一个文件下没什么区别(main.h都既能识别factorynew的声明也能识别factorynew的定义)。
于是我在分开的情况下用template<class _Pro> DECLARE_FACTORY_OF(_Pro);
来初始化静态指针,发现有错误。
想到LS我曾经把factorynew放到一起也是用template<class _Pro> DECLARE_FACTORY_OF(_Pro);
来初始化成员变量,也是出现这个错误,所以我知道了这个错误大概不是因为factorynew有没有合并造成的,而是template<class _Pro> DECLARE_FACTORY_OF(_Pro);
这句话本身就有问题。
于是我改用template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);
来初始化静态指针,发现在合并的情况下没有报错,成功做到了要求。
随后我又尝试在没有合并的情况下用template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);
来初始化静态指针,结果发现也没有报错。
这样一来就发现了错误:
在FactoryNew中用下面这个来初始化成员静态指针是多此一举且错误的!!
#define DECLARE_FACTORY_OF(_Pro)\
FactoryNew<_Pro>* Singleton<FactoryNew<_Pro>>::_instance=NULL
template<class _Pro> DECLARE_FACTORY_OF(_Pro);
在Singleton中用下面这个来初始化成员静态指针是正确的。
#define DECLARE_SINGLETON_MEMBER(_Ty)\
_Ty* Singleton<_Ty>::_instance=NULL
template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);
template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);
就没有报错了。
然后,我还有疑问,为什么将.h和.cpp文件合并后就不会出错?[/quote]
模板的定义一般都放在头文件中的,便于模板实例化。 template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);
就没有报错了。
然后,我还有疑问,为什么将.h和.cpp文件合并后就不会出错?