【请教】继承了单例模式类模板的工厂模式类模板,怎么一次性初始化所有模板实例的成员静态指针

resurvive resurvive 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我下写了一个单例的工厂类Factory
#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

这个Factory工厂不用为每一个产品(ProductA,ProductB)对应的工厂分别初始化静态指针,而是可以用template来一次性为所有模板实例化的工厂初始化静态指针

然后写了一个单例模板类Singleton,用别的类继承这个单例模板来新建单例更加便捷,因为我的程序里要有很多个单例,所以我倾向于用这种方法产生出来的下面的单例工厂FactoryNew(比较好管理),而不是上面的单独写出来的单例工厂Factory
#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__


新写了一个工厂类FactoryNew,继承单例模板Singleton
#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);
*/

但这个FactoryNew就不能用template方法为一次性为所有工厂的模板实例初始化静态指针

对于产品类ProductA,ProductB,ProductC,ProductD
#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


main.h如下
#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;
}

FactoryNew必须如main.h中这样分别初始化每个实例的静态指针

如果FactoryNew也用上面注释掉的方法初始化静态指针就会报错:
1> main.cpp
1>e:\workspace\visual studio\c++\factorytemplate\factorytemplate\factorynew.cpp(38): error C3860: 模板 参数列表(在类 模板 名称后面)必须按 模板 参数列表中使用的顺序列出参数
1> FactoryNew.cpp
1>e:\workspace\visual studio\c++\factorytemplate\factorytemplate\factorynew.cpp(38): error C3860: 模板 参数列表(在类 模板 名称后面)必须按 模板 参数列表中使用的顺序列出参数
请教,为什么FactoryNew不能像Factory一样用类似方法初始化静态指针,另外FactoryNew有没有简单的方法来初始化静态指针,而不用为每个模板实例都初始化一遍?
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
resurvive
C++ 语言 65,213 成员 250,513 内容