设计模式之单例模式

本文深入探讨设计模式中的单例模式,解析其实现原理及应用,包括不考虑线程安全的基本单例类,以及懒汉模式和饿汉模式下的线程安全实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计模式

设计模式代表了最佳实践,是软件开发过程中面临一般问题的解决方法
设计模式是一套被反复使用,经过分类,代码设计的经验总结

单例模式
  • 单例类保证全局只有一个唯一实例对象
  • 单例类提供获取这个唯一实例的接口
不考虑线程安全的一个单例类
#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;

class Singleton 
{
  public:
    //获取唯一对象实例的接口函数
    static  Singleton* GetInstance()
    {
      if(_sInstance==NULL)
      {
        _sInstance=new Singleton;
      }
      return _sInstance;
    }
  protected:
    Singleton()
      :_data(0)
    {}
    //防拷贝
    Singleton(Singleton&);
    Singleton&operator=(Singleton&);
    static Singleton* _sInstance;
    int _data;
};
懒汉模式
#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;

//线程安全版本
//1.懒汉模式

class Singleton 
{
  public:
    //获取唯一对象实例的接口函数
    static  Singleton* GetInstance()
    {
      lock();
      if(_sInstance==NULL)
      {
        _sInstance=new Singleton;
      }
      unlock();
      return _sInstance;
    }
  protected:
    Singleton()
      :_data(0)
    {}
    //防拷贝
    Singleton(Singleton&);
    Singleton&operator=(Singleton&);
    static Singleton* _sInstance;
    int _data;
};

Singletion* Singleton::_sInstance=NULL;
双重检查版本更加高效
#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<mutex>
using namespace std;

//线程安全版本
//1.懒汉模式

class Singleton 
{
  public:
    //获取唯一对象实例的接口函数
    static  Singleton* GetInstance()
      if(_sInstance==NULL)
      {
        //lock();
        //_mtx.lock();
        lock_guard<mutex> lock(_mtx);
        if(_sInstance==NULL)
        {
          _sInstance=new Singleton;
        }
        //unlock();
        //_mtx.unlock();
        return _sInstance;
      }
}
protected:
Singleton()
  :_data(0)
{}
//防拷贝
Singleton(Singleton&);
Singleton&operator=(Singleton&);
static Singleton* _sInstance;
static mutex _mtx;
int _data;

};
mutex Singletion::_mtx;
Singletion* Singleton::_sInstance=NULL;
饿汉模式
#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<mutex>
using namespace std;

//线程安全版本
//1.饿汉模式

class Singleton 
{
  public:
    //获取唯一对象实例的接口函数
    static  Singleton* GetInstance()
    {
    assert(_sInstance);
    return _sInstance;
    }
      if(_sInstance==NULL)
      {
        if(_sInstance==NULL)
        {
          _sInstance=new Singleton;
        }
        return _sInstance;
      }
}
protected:
Singleton()
  :_data(0)
{}
//防拷贝
Singleton(Singleton&);
Singleton&operator=(Singleton&);
static Singleton* _sInstance;
int _data;

};
Singletion* Singleton::_sInstance=new Sigleton();

#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<mutex>
using namespace std;

//线程安全版本
//1.饿汉模式

class Singleton 
{
  public:
    //获取唯一对象实例的接口函数
    static  Singleton* GetInstance()
    {
    static Singleton tmp;
    return &tmp;
    }
      if(_sInstance==NULL)
      {
        if(_sInstance==NULL)
        {
          _sInstance=new Singleton;
        }
        return _sInstance;
      }
}
protected:
Singleton()
  :_data(0)
{}
//防拷贝
Singleton(Singleton&);
Singleton&operator=(Singleton&);
int _data;

};
Singletion* Singleton::_sInstance=new Sigleton();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值