问题描述
在c ++中优雅的设计模式(GoF模式)实现是什么?
任何人都可以给我一些基于模板的设计模式实现)?
示例(基于模板的Singleton): -
;类型名称T>
类Singleton:public boost :: noncopyable
{
public:
static Singleton& GetInstance()
{
boost :: call_once(& CreateInstance,m_onceFlg);
return * m_pInstance;
}
virtual〜Singleton()
{
}
protected:
Singleton()
{
} $ b b static void CreateInstance()
{
m_pInstance.reset(new T());
}
private:
static boost :: once_flag m_onceFlg;
static boost :: scoped_ptr< T> m_pInstance;
};
根据我的经验,模板库。设计模式很难正确捕获为一个具体的模板,没有注意事项或强加难以强加对试图插入自己的类的限制。
让你的 Singleton
示例。好吧,我实际上要重写它,以便我不必让Boost使用它:
template< ;类型名称T>
class Singleton {
Singleton(const Singleton< T>&)= delete;
Singleton& operator =(const Singleton T&)= delete;
static Singleton< T> & GetInstanceInternal(){
static T instance;
return instance;
}
protected:
Singleton(){}
〜Singleton(){}
public:
static T& GetInstance(){
return static_cast< T&>(GetInstanceInternal());
}
};
虽然这使 Singleton< T>
singleton,真正想要的是使 T
一个单例。好吧,你可能会说,这没有问题,因为用法是 T
应该继承 Singleton< T>
(由 Singleton :: GetInstanceInternal()
)强制执行:
Foo:public Singleton< Foo> {
public:
void foo(){/*...*/}
};
一个朴素的程序员会认为Job done!,因为 Foo
继承自 Singleton< Foo>
,使 Foo
单例。但它不会,因为它不能防止这种情况:
Foo x;
为了解决这个问题,构造函数应该是私有的(因此, Singleton< ; Foo>
需要成为朋友)。为了防止直接调用析构函数,它也应该是私有的。
class Foo:public Singleton< Foo> {
friend class Singleton< Foo> ;;
Foo(){}
〜Foo(){}
public:
void foo(){/*...*/}
};
因此,除了继承 Singleton< Foo>
有额外的要求,单独的继承不能正确执行。所有这些要求都可以被记录,但是可以说,使用模板变得不那么有用,并且直接将单例功能放入 Foo
中的工作几乎一样。 / p>
What are the elegant design pattern(GoF patterns) implementation in c++ ?
Can anyone give me some examples of design pattern implementations based on template(which can be reused) ?
Example(Template based 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;
};
In my experience, there are really no good design pattern template libraries. A design pattern is difficult to capture correctly as a concrete template without caveats or imposing difficult to enforce restrictions on the classes that try to plug themselves into it.
Let's take your Singleton
example. Well, I'm actually going to rewrite it so that I don't have to have Boost to use it:
template <typename T>
class Singleton {
Singleton (const Singleton<T> &) = delete;
Singleton & operator = (const Singleton<T> &) = delete;
static Singleton<T> & GetInstanceInternal () {
static T instance;
return instance;
}
protected:
Singleton () {}
~Singleton () {}
public:
static T & GetInstance () {
return static_cast<T &>(GetInstanceInternal());
}
};
Although this makes Singleton<T>
a singleton, what is really wanted is to make the T
a singleton. Well, you might say, this is no problem, because the usage is that T
should inherit from Singleton<T>
(as enforced by Singleton::GetInstanceInternal()
):
class Foo : public Singleton<Foo> {
public:
void foo () { /*...*/ }
};
A naive programmer would think "Job done!", because since Foo
inherits from Singleton<Foo>
, that makes Foo
a singleton. But it doesn't, because it doesn't prevent this:
Foo x;
To fix this, the constructor should be made private (and therefore, Singleton<Foo>
needs to be made a friend). To prevent calling the destructor directly, it should be made private as well.
class Foo : public Singleton<Foo> {
friend class Singleton<Foo>;
Foo () {}
~Foo () {}
public:
void foo () { /*...*/ }
};
So, in addition to inheriting Singleton<Foo>
there are additional requirements that inheritance alone cannot properly enforce. All these requirements can be documented, but it can be argued that using the templates become less useful, and it is almost as much work as putting the singleton functionality into Foo
directly.
这篇关于设计模式(GoF模式)在c ++中的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!