当前位置:   article > 正文

C++模板——单例类模板_c++ 单例模板

c++ 单例模板

本文参照于狄泰软件学院——《C++深度剖析课程》

需求:在架构设计时,一些类在整个系统声明周期中最多只能有一个对象存在。那么我们如何定义一个类,使得这个类最多只能创建一个对象呢?

单例模式

  1. 要控制类的对象数目,必须隐藏构造函数
  2. 定义一个静态对象指针instance并初始化为NULL,当需要创建对象时,先判断instance的值:
    a) 当instance为空值时:创建对象,并用instance标记
    b) 当instance为非空值时,返回instance标记的对象。

示例代码:单例模式初探

#include <iostream>
#include <string>

using namespace std;

class SObject
{
    static SObject* c_instance;

    SObject(const SObject&);
    SObject& operator= (const SObject&);

    SObject()
    {
    }
public:
    static SObject* GetInstance();

    void print()
    {
        cout << "this = " << this << endl;
    }
};

SObject* SObject::c_instance = NULL;

SObject* SObject::GetInstance()
{
    if( c_instance == NULL )
    {
        c_instance = new SObject();
    }

    return c_instance;
}

int main()
{
    SObject* s = SObject::GetInstance();
    SObject* s1 = SObject::GetInstance();
    SObject* s2 = SObject::GetInstance();

    s->print();
    s1->print();
    s2->print();

    return 0;
}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

输出结果:
this = 0x9674008
this = 0x9674008
this = 0x9674008

我们发现,无论创建多少个对象,实际对象只有一个!

存在的问题
  1. 必须定义静态成员变量c_instance
  2. 必须定义静态成员函数GetInstance()

问题:如何解决这个问题呢?

我们可以将单例模式相关的代码抽取出来,开发单例类模板。当需要使用单例类模板时,可以申请成为单例类模板的友元类。这样就可以直接成为单例模式了。

示例代码:

//Singleton.h
#ifndef _SINGLETON_H_
#define _SINGLETON_H_

template
< typename T >
class Singleton
{
    static T* c_instance;
public:
    static T* GetInstance();
};

template
< typename T >
T* Singleton<T>::c_instance = NULL;

template
< typename T >
T* Singleton<T>::GetInstance()
{
    if( c_instance == NULL )
    {
        c_instance = new T();
    }

    return c_instance;
}


#endif


//main.cpp
#include <iostream>
#include <string>
#include "Singleton.h"
using namespace std;

class SObject
{
    friend class Singleton<SObject>;    // 当前类需要使用单例模式

    SObject(const SObject&);
    SObject& operator= (const SObject&);

    SObject()
    {
    }
public:
    void print()
    {
        cout << "this = " << this << endl;
    }
};

int main()
{
    SObject* s = Singleton<SObject>::GetInstance();
    SObject* s1 = Singleton<SObject>::GetInstance();
    SObject* s2 = Singleton<SObject>::GetInstance();

    s->print();
    s1->print();
    s2->print();

    return 0;
}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/137769
推荐阅读
相关标签
  

闽ICP备14008679号