【设计模式】单例模式的两种实现以及特殊类的设计

实现单例模式

单例模式是一种设计模式,在实战中经常会用到。其意思是创建一个类这个类只能唯一的创建一个对象,如果之后还想用这个类创建新对象的时候都会返回最开始创建的呢个对象。

要实现这一点有两种思路,分别成为懒汉模式和饿汉模式。

饿汉模式

饿汉模式是在程序启动时就夹在所有需要资源的设计模式,不涉及线程安全的问题,用这种思想实现单例模式时需要在程序一开始就直接声明对象,需要使用对象就返回对象即可。但是坏处是程序启动时会消耗时间可能会造成程序启动缓慢。以空间换时间的思想。

成员变量静态化——资源共享;构造函数私有化。

#include <iostream>
class Singleton1
{
public:
    //外留接口
    static Singleton1* GetInstence()
    {
        return &_singleton;
    }
private:
    //防拷贝,禁用构造,拷贝构造和赋值
    Singleton1()
    {

    }
    Singleton1(const Singleton1&);
    Singleton1& operator=(const Singleton1&);
    static Singleton1 _singleton;
};
Singleton1 Singleton1::_singleton;
懒汉模式

懒汉模式就是在第一次使用时才会创建对象,因此我们需要对变量进行判断考虑到线程安全我们需要对其加锁。懒汉模式的坏处是可能造成程序运行卡顿。

资源使用静态指针——资源共享,延时加载。

二次判断,减少锁冲突,提高效率。

class Singleton2
{
public:
    static Singleton2* GetInstence()
    {
        //双重判断避免不必要的锁竞争
        if(_pSingleton == nullptr)
        {
            //为了线程安全需要加锁
            _mtx.lock();
            if (_pSingleton == nullptr)
            {
                _pSingleton = new Singleton2;
            }
            _mtx.unlock();
        }
        return _pSingleton;
    }
private:
    Singleton2(){}
    Singleton2(const Singleton2&);
    Singleton2& operator=(const Singleton2&);
    static std::mutex _mtx;
    static Singleton2* _pSingleton;
};
std::mutex Singleton2::_mtx;
Singleton2* Singleton2::_pSingleton = nullptr;

写一个只能在堆上创建对象的类

思路
1、将构造函数,赋值构造函数全部封装为私有,不允许外部直接调用构造。

2、单独写一个静态函数提供在堆上创建对象的接口。

实现

#include <iostream>
using namespace std;
class HeapOnly
{
  public:
    //开放构造接口                        
    static HeapOnly* Create()
    {
      cout << "create int heap" << endl;
      return new HeapOnly();
    }
    ~HeapOnly()
    {
      cout << "destorying" << endl;
    }
  private:
    //构造函数私有化
    HeapOnly(){}
    HeapOnly(const HeapOnly&){}
};
int main()
{
  HeapOnly* heapOnly = HeapOnly::Create();
  delete heapOnly;
}


[root@localhost 内存管理]$ ./HeapOnlyClass
create int heap
destorying

写一个只能在栈上创建对象的类

思路一
1、将构造函数,赋值构造函数全部封装为私有,不允许外部直接调用构造。

2、单独写一个静态函数提供在栈上创建对象的接口。

#include <iostream>
using namespace std;
class StackOnly
{
  public:
    static StackOnly Create()
    {
      //创建匿名对象
      return StackOnly();
    }
    static StackOnly a;
    ~StackOnly()
    {
      cout << "destorying" << endl;      
    }
  private:
    StackOnly()
    {
      cout << "create in stack" << endl;
    }
    StackOnly(const StackOnly&){}
};
int main()
{
  StackOnly::a = StackOnly::Create();
}


[root@localhost 内存管理]$ ./StackOnlyClass 
create in stack
destorying

思路二
  1、直接将operator new和operator delete重载并定义为私有。

#include <iostream>                     
using namespace std;               
class StackOnly                  
{
  public:      
    StackOnly()
    {    
      cout << "create in stack" << endl;
    }
    ~StackOnly()    
    {                    
      cout << "destorying" << endl;
    }                  
  private:      
    void* operator new(size_t size){}
    void operator delete(void* p){}
};   


int main()
{                   
  StackOnly p;    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值