基于模板和继承的单例模式

单例模式的一小段笔记

上个学期写了个python3解释器,里面抽象语法树的工厂单例是这样实现的:

1
2
3
4
AstFactory& AstFactory::getinstance() {
    static AstFactory theinstance;
    return theinstance;
}

基于模板的方法比较直观,不用每个类写一遍: (dtk的例子)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*!
 * a simple singleton template for std c++ 11 or later.
 *
 * example:
 *
 *  class ExampleSingleton : public QObject, public Dtk::DSingleton<ExampleSingleton>
 *  {
 *      Q_OBJECT
 *      friend class DSingleton<ExampleSingleton>;
 *  };
 *
 * Warning: for Qt, "public DSingleton<LyricService>" must be after QObject.
 */

template <class T>
class DSingleton
{
public:
    static inline T *instance()
    {
        static T  *_instance = new T;
        return _instance;
    }

protected:
    DSingleton(void) {}
    ~DSingleton(void) {}
    DSingleton(const DSingleton &) {}
    DSingleton &operator= (const DSingleton &) {}
};
发表于 2018-07-30
JS
Arrow Up