c++ - C++ 中的基本单例对象

标签 c++ design-patterns singleton

这是创建基本单例对象以确保其所有子对象也是单例的合法方法吗?我想结合工厂模式使用它来确保所有工厂都是单例。

关键类是为了防止 children 绕过单例构造函数,但基本上只是一个形式参数。

这种方法有什么特别的问题吗?

class singleton
{
protected:
    struct key
    {
    private:
        friend class singleton;

        key(){}
    };

public:
    singleton(const key&)
    {}

    template <class child> static child* getInstance()
    {
        static key    instanceKey;
        static child* unique = new child(instanceKey);

        return unique;
    }

private:
};

class test : public singleton
{
public:
    test(singleton::key& key)
        : singleton(key)
    {}

    void init()
    {
        //init object
    }

private:
};

int main()
{
    test* t = singleton::getInstance<test>();

    return 0;
}

最佳答案

我在最后添加了一个类来删除你所有的工厂。 :) 你需要一个虚拟析构函数链来实现这个机制。除此之外,我还没有发现其他内存泄漏并且实例是唯一的。带有“ key ”的安全机制似乎也很好。我看不出有什么办法可以在静态函数之外获取 key 。

#include <iostream>
#include <set>

class Singleton;

class Set_of_Singletons
{
    friend class Singleton;
private:
    std::set<Singleton*> instances;

    Set_of_Singletons():instances(){}
public:
    ~Set_of_Singletons();
};

class Singleton
{
private:
    static Set_of_Singletons  children;

protected:
    struct key
    {
    private:
        friend class Singleton;

        key(){}
    };    
public:
    template <class Child> static Child* doNew()
    {
        static key    instanceKey;
        Child* u = new Child(instanceKey);
        children.instances.insert((Singleton*)u);
        return u;
    }

    template <class Child> static Child* getInstance()
    {
        static Child* unique = doNew<Child>();
        return unique;
    }

    Singleton(const key&)
    {}

    virtual ~Singleton(){}
};

Set_of_Singletons::~Set_of_Singletons()
{
    for (auto inst: instances)
        delete inst;

    instances.clear();
}

Set_of_Singletons Singleton::children;

class B: public Singleton 
{
public:
    B(Singleton::key& key)
        : Singleton(key)
    {
        std::cout << ">>>> Construction of B \n";    
    }

    virtual ~B()
    {
        std::cout << "<<<< Destruction of B \n";    
    }

};

class C final: public Singleton 
{
public:
    C(Singleton::key& key)
        : Singleton(key)
    {
        std::cout << ">>>> Construction of C \n";
    }

    virtual ~C()
    {
        std::cout << "<<<< Destruction of C \n";    
    }
};

int main()
{
    // Object creation seems all ok
    B* x = Singleton::getInstance<B>();
    std::cout << "x: " << x << "\n";    

    B* y = Singleton::getInstance<B>();
    std::cout << "y: " << y << "\n";

    C* v = Singleton::getInstance<C>();
    std::cout << "v: " << v << "\n";

    C* w = Singleton::getInstance<C>();
    std::cout << "w: " << w << "\n";
    return 0;
}

// ~Have fun.~

关于c++ - C++ 中的基本单例对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45500487/

相关文章:

c++ - NTL - 如何获取 GF(2^n) 中元素的整数表示

Java:没有接口(interface)实现?

oop - 观察者模式的反面是什么?

c++ - 语法错误 : identifier `MercedesFactory` ?

java - spring容器的垃圾收集

c++ - gcc - 如何找到头文件包含文件的路径

c++ - Visual Studio 2019 C++ dll Excel VBA 插件问题

ios - 具有核心数据对象 null 的 NSArray 的单例

c# - 使用 C# 在 Mysql 上出现死锁 - "Lock wait timeout exceeded; try restarting transaction"

c++ - g++ 总是向后兼容 "older"静态库?