C++ 单例模板设计问题
我目前正在使用以下简单的单例类:C++ 单例模板设计问题,c++,design-patterns,singleton,C++,Design Patterns,Singleton,我目前正在使用以下简单的单例类: template<class T> class singleton : boost::noncopyable { public: static T& get_instance() { assert(sm_instance != nullptr); return *static_cast<T*>(sm_instance); } prot
template<class T>
class singleton
: boost::noncopyable
{
public:
static T& get_instance()
{
assert(sm_instance != nullptr);
return *static_cast<T*>(sm_instance);
}
protected:
singleton()
{
assert(sm_instance == nullptr);
sm_instance = this;
}
virtual ~singleton()
{
assert(sm_instance != nullptr);
sm_instance = nullptr;
}
private:
static singleton<T>* sm_instance;
};
template<class T> singleton<T>* singleton<T>::sm_instance = nullptr;
class example_one
: public singleton<example_one>
{
static example_one instance;
};
example_one example_one::instance;
class example_two
: singleton<example_two>
{
static example_two instance;
};
example_two example_two::instance;
// Usage:
example_one& x = example_one::get_instance();
example_two& y = example_two::get_instance(); // not accessible because 'example_two' uses 'private' to inherit from 'singleton<T>'
如果要使用模板函数,为什么还要处理基类:
template<typename T>
T& getInstance()
{
static T instance;
return instance;
}
class example_one: boost::noncopyable
{
example_one() {} // Note private construtor
friend example_one& getInstance<example_one>();
};
class example_two: boost::noncopyable
{
example_two() {}
friend example_two& getInstance<example_two>();
};
模板
T&getInstance()
{
静态T实例;
返回实例;
}
类示例一:boost::noncopyable
{
示例_one(){}//注意私有构造函数
朋友示例_one&getInstance();
};
类示例二:boost::noncopyable
{
示例_two(){}
朋友示例二&getInstance();
};
由Andrei Alexandrescu推广的解决方案使用了一种不同的技术,与此技术相比,它有几个优点(其中之一是您想要进行的调整)。它在上提供,但是您可能想下载整个库(loki),或者如果不需要额外的东西,可以稍微清理一下。然后,界面变为:
typedef Loki::SingletonHolder< SomeType > SomeTypeSingleton;
SomeTypeSingleton::Instance(); // Access the single instance
typedef-Loki::SingletonHolder< SomeType>SomeTypeSingleton;
SomeTypeSingleton::Instance();//访问单个实例
这比我的方法更不灵活。您需要一个
friend
声明、一个私有副本构造函数和赋值运算符,并且您不能将编译时常量传递给类的构造函数。不,我也不会这样做(因为您不会在我的代码中找到Singleton)。但你的论点都有缺陷。1) 这是一个单独的构造函数,你不需要向构造函数传递任何东西,因为只有一个构造函数,所以所有的初始化都是在构造函数中完成的,你不需要传递任何东西。2) 所有单子都需要专用构造函数/复制构造函数和赋值运算符。3) 我的班级有一行额外的代码给我的朋友。您定义了一个不必要的锅炉板子类。我想你会发现我的衣服更整洁,更容易保养。即使Singelton一开始是个完全糟糕的主意。如果我要构建一个singleton,我会使用经典的Myers singleton。我这样做是因为您试图对样板文件进行泛化。如果您可以以任何方式避免它,请不要使用单例-您的代码没有多大意义。第一个示例检查其实例字段在构造函数中是否为null(为什么?),然后检查析构函数是否更糟糕。您的析构函数应该删除sm_实例,或者根本不需要析构函数。同样,将析构函数中的instacne字段设置为null也是毫无意义的。@AngelO'Sphere-我认为您应该重新阅读代码并考虑一下。
typedef Loki::SingletonHolder< SomeType > SomeTypeSingleton;
SomeTypeSingleton::Instance(); // Access the single instance