C++单例模式的实现及举例

单例模式的概念和用途:

在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便实例个数的控制并节约系统资源。

如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。(直白地说单例模式的写法也是种套路,见例子便知。)

 

举例:

1. 主席   

目的:为了让类中只有一个实例,实例不需要自己释放


• 将 默认构造 和 拷贝构造 私有化
• 内部维护一个 对象指针
• 私有化唯一指针
• 对外提供getinstance方法来访问这个指针
• 保证类中只能实例化一个对象

#include<iostream>
using namespace std;

//创建主席类
//需求 单例模式 为了创建类中的对象,并且保证只有一个对象实例
class ChairMan {
private:
    ChairMan()
    {
        cout << "创建主席" << endl;
    }
    //拷贝构造 私有化
    ChairMan(const ChairMan&c) {}
public:
    //提供 get方法 访问 主席
    static ChairMan* getInstance() {
        return singleMan;
    }


private:
    static ChairMan * singleMan;
};
ChairMan * ChairMan::singleMan = new ChairMan;

void test01() {
    ChairMan *cm1 = ChairMan::getInstance();
    ChairMan *cm2 = ChairMan::getInstance();
    if (cm1 == cm2) {
        cout << "cm1与cm2相同" << endl;
    }
    else cout << "cm1与cm2不同" << endl;

    /*ChairMan *cm3 = new ChairMan(*cm2);
    if (cm3 == cm2) {
        cout << "cm3与cm2相同" << endl;
    }
    else cout << "cm3与cm2不同" << endl;*/
}

int main() {

    cout << "main调用" << endl;  //主席创建先于main调用
    test01();

    system("pause");
    return 0;
}

 


2. 打印机

用单例模式,模拟公司员工使用打印机场景,打印机可以打印员工要输出的内容,并且可以累积打印机使用次数。

#include<iostream>
#include<string>
using namespace std;

class Printer {
private:
    Printer() { m_Count = 0; };
    Printer(const Printer& p);

public:
    static Printer* getInstance() {
        return singlePrinter;
    }

    void printText(string text) {
        cout << text << endl;
        m_Count++;
        cout << "打印机使用次数:" << m_Count << endl;
    }

private:
    static Printer* singlePrinter;
    int m_Count;
};

Printer* Printer::singlePrinter = new Printer;

void test1() {
    //拿到打印机
    Printer* printer = Printer::getInstance();

    printer->printText("离职报告");
}

int main() {
    test1();

    system("pause");
    return 0;
}

 

3. 模板

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Singleton
 6 {
 7 private:
 8     Singleton()
 9     {
10         cout << "Singleton" << endl;
11     }
12     ~Singleton()
13     {
14         cout << "~Singleton" << endl;
15     }
16 
17 private:
18     static Singleton* _pInstance;
19 
20 public:
21     static Singleton * getInstance()
22     {
23         if(nullptr == _pInstance)
24         {
25             _pInstance = new Singleton;
26         }
27         return _pInstance;
28     }
29     static void destroy()
30     {
31         if(_pInstance)
32         {
33             delete _pInstance;
34         }
35     }
36     void print() const
37     {
38         cout << "Singleton::print()" << endl;
39     }
40 };
41 
42 Singleton* Singleton::_pInstance = nullptr;
43 
44 int main()
45 {
46     Singleton * p1 = Singleton::getInstance();
47     Singleton * p2 = Singleton::getInstance();
48 
49     p1->print();
50     p2->print();
51     Singleton::getInstance()->print();
52     Singleton::destroy();
53 
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/Mered1th/p/10574585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值