C++中优雅的设计模式(GoF模式)实现是什么?C++中的设计模式(GoF模式)实现
任何人都可以给我一些基于模板(可以重用)的设计模式实现的例子吗?
例(基于模板的Singleton): -
template<typename T>
class Singleton : public boost::noncopyable
{
public:
static Singleton& GetInstance()
{
boost::call_once(&CreateInstance, m_onceFlg);
return *m_pInstance;
}
virtual ~Singleton()
{
}
protected:
Singleton()
{
}
static void CreateInstance()
{
m_pInstance.reset(new T());
}
private:
static boost::once_flag m_onceFlg;
static boost::scoped_ptr<T> m_pInstance;
};
我不一定会调用基于boost的C++单例实现,非常优雅... –
呃,阅读GoF书吗?本书中的大部分示例都是C++。 – PherricOxide
@Andreas Grapentin Qt的人不喜欢使用boost库。这里我举了一个例子。 –