c++单例模式

1.基本实现

class Singleton
{
public:
    static Singleton *Instance()
    {
        if (!spInstance)
        {
            spInstance = new Singleton;
        }

        return spInstance;
    }

protected:
    Singleton() { }
    ~Singleton()
    {
        if (spInstance)
        {
            delete spInstance;
        }
    }

private:
    static Singleton *spInstance;
};

Singleton *Singleton::spInstance = nullptr;

2.单例模板
singleton.h

#ifndef SINGLETON_H
#define SINGLETON_H

template<typename T>
class Singleton
{
public:
    static T &Instance()
    {
        static T sInstance;
        return sInstance;
    }

private:
    Singleton() { }
    Singleton(const Singleton &);
    Singleton &operator=(const Singleton &);
};

#endif

main.cpp

#include <iostream>
#include "singleton.h"

using namespace std;

class Example
{
public:
    Example() { }
    ~Example() { }

    void Print();
};

void Example::Print()
{
    cout << "Example.Print()" << endl;
}

int main()
{
    Singleton<Example>::Instance().Print();
}

3.改进的单例模板

singleton.h

#ifndef SINGLETON_H
#define SINGLETON_H

template<typename T>
class Singleton
{
public:
    static T &Instance()
    {
        static T sInstance;
        return sInstance;
    }

protected:
    Singleton() { }
    virtual ~Singleton() { }

private:
    Singleton(const Singleton &);
    Singleton &operator=(const Singleton &);
};

#endif

main.cpp

#include <iostream>
#include "singleton.h"

using namespace std;

class Example : public Singleton<Example>
{
    friend class Singleton<Example>;

public:
    void Print();

private:
    Example() { }
    virtual ~Example() { }
};

void Example::Print()
{
    cout << "Example.Print()" << endl;
}


int main()
{
    Example::Instance().Print();
}

4.带参数的单例模板
singleton.h

#ifndef SINGLETON_H
#define SINGLETON_H

#include <utility>

template<typename T>
class Singleton
{
public:
    template<typename... Args>
    static void CreateInstance(Args&&... args)
    {
        spInstance = new T(std::forward<Args>(args)...);
    }

    static T *Instance()
    {
        return spInstance;
    }

    static void DestroyInstance()
    {
        if (spInstance)
        {
            delete spInstance;
            spInstance = nullptr;
        }
    }

protected:
    Singleton() { }
    virtual ~Singleton() { }

private:
    Singleton(const Singleton &);
    Singleton &operator=(const Singleton &);

private:
    static T *spInstance;
};

template<typename T>
T *Singleton<T>::spInstance = nullptr;

#endif

main.cpp

#include <iostream>
#include "singleton.h"

using namespace std;

class Example : public Singleton<Example>
{
    friend class Singleton<Example>;

public:
    void Print();

private:
    Example(int i) { member = i; }
    virtual ~Example() { }

private:
    int member;
};

void Example::Print()
{
    cout << "Example.Print(), member = " << member << endl;
}

int main()
{
    Example::CreateInstance(3);

    Example::Instance()->Print();

    Example::DestroyInstance();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值