首页 > 代码库 > 设计模式
设计模式
设计模式
设计模式代表了最佳实践,是软件开发过程中面临一般问题的解决方案
设计模式是一套被反复使用,经过分类,代码设计的经验
单例模式
单例模式也叫单件模式Singleton是一个非常用的设计模式,建一个线程安全且高效的Singleton是非常重要的
1.不考虑线程安全的一个单例模式
class Singleton { public: static Singleton* GetInstance() { if (_sInstance == NULL) { _sInstance = new Singleton(); } return _sInstance; } static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: //定义构造函数私有,限制只能在类内创建对象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); //指向实例的指针为静态私有,这样定义静态成员函数获取对象实例 static Singleton* _sInstance; int _data; };
2.线程安全单例
懒汉模式
class Singleton { public: static Singleton* GetInstance() { if(_sInstance==NULL) { lock(); if (_sInstance == NULL) { _sInstance = new Singleton(); } unlock(); return _sInstance; } static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: //定义构造函数私有,限制只能在类内创建对象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); //指向实例的指针为静态私有,这样定义静态成员函数获取对象实例 static Singleton* _sInstance; int _data; }; //静态的数据成员必须初始化 Singleton* Singleton::_sInstance = NULL; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
第二个版本
class Singleton { public: static Singleton* GetInstance() { if (_sInstance == NULL) { _sInstance = new Singleton(); } return _sInstance; } static void DelInstance() { std::lock_guard<std::mutex> lock(_mtx); if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: //定义构造函数私有,限制只能在类内创建对象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); //指向实例的指针为静态私有,这样定义静态成员函数获取对象实例 static Singleton* _sInstance; int _data; }; //静态的数据成员必须初始化 Singleton* Singleton::_sInstance = NULL; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
饿汉模式
方式一
class Singleton { public: // 获取唯一对象实例的接口函数 static Singleton* GetInstance() { static Singleton tmp; return &tmp; } void Print() { cout<<_data<<endl; } private: // 构造函数定义为私有,限制只能在类内创建对象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); // 单例类里面的数据 int _data; }; void TestSingleton() { Singleton::GetInstance()->Print(); }
方式二
class Singleton { public: // 获取唯一对象实例的接口函数 static Singleton* GetInstance() { assert(_sInstance); return _sInstance; } // 删除实例对象 static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: // 构造函数定义为私有,限制只能在类内创建对象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); // 指向实例的指针定义为静态私有,这样定义静态成员函数获取对象实例 static Singleton* _sInstance; // 单例类里面的数据 int _data; }; Singleton* Singleton::_sInstance = new Singleton; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
在堆上创建对象
class AA { public: AA* GetObj() { return new AA; } private: AA() {} AA(const AA&); int a; };
在栈上创建对象
方式一
class AA { public: AA GetObj() { return AA(); } private: AA() {} AA(const AA&); int a; };
方式二
class AA { private: void*operator new(size_t); void operator delete(void*p); void*operator new[](size_t); void operator delete[](void* p); private: AA() {} AA(const AA&); int a; };
设计模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。