简体   繁体   English

c ++中的设计模式(GoF模式)实现

[英]design pattern(GoF patterns) implementation in c++

What are the elegant design pattern(GoF patterns) implementation in c++ ? 在c ++中实现优雅的设计模式(GoF模式)是什么?

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;
};

Take a look at Modern C++ Design by Alexandrescu 看看Alexandrescu的Modern C ++ Design

http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315 http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315

He covers template implementation of several design patterns. 他介绍了几种设计模式的模板实现。 In fact, IIRC, one of the forewards is written by one of the GOF. 事实上,IIRC,其中一个前锋是由一个GOF写的。

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. 我们来看看你的Singleton例子吧。 Well, I'm actually going to rewrite it so that I don't have to have Boost to use it: 好吧,我实际上要重写它,以便我不必让Boost使用它:

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. 虽然这使Singleton<T>成为单身,但真正想要的是使T成为单身。 Well, you might say, this is no problem, because the usage is that T should inherit from Singleton<T> (as enforced by Singleton::GetInstanceInternal() ): 好吧,你可能会说,这没问题,因为用法是T应该继承自Singleton<T> (由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. 一个天真的程序员会认为“Job done!”,因为自从Foo继承了Singleton<Foo> ,这使得Foo成为一个单身人士。 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). 要解决这个问题,应该将构造函数设置为私有(因此, Singleton<Foo>需要成为朋友)。 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. 因此,除了继承Singleton<Foo>之外,还有一些额外的要求,即单独的继承无法正确执行。 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. 所有这些要求都可以记录下来,但可以说使用模板变得不那么有用,并且将单例功能直接放入Foo几乎同样重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM