当前位置: 首页 > news >正文

鸿蒙系统app开发培训优化

鸿蒙系统app开发,培训优化,vue框架做的网站,不用编程做网站一、单例模式 单例模式(Singleton Pattern),使用最广泛的设计模式之一。其意图是保证一个类仅有一个实例被构造,并提供一个访问它的全局访问接口,该实例被程序的所有模块共享。 1、饿汉式 1.1、基础版本 在程序启动后立刻构造单例&#xff0…

一、单例模式

单例模式(Singleton Pattern),使用最广泛的设计模式之一。其意图是保证一个类仅有一个实例被构造,并提供一个访问它的全局访问接口,该实例被程序的所有模块共享。

1、饿汉式

1.1、基础版本

在程序启动后立刻构造单例,饿汉式实现一个单例类步骤如下:

  • 定义一个单例类
  • 私有化构造函数,防止外界直接创建单例类的对象
  • 禁用拷贝构造,移动赋值等函数,可以私有化,也可以直接使用=delete
  • 使用一个公有的静态方法获取该实例
  • 确保在第一次调用之前该实例被构造

代码实现

#include <iostream>
#include <string>
using namespace std;// 单例类
class Singleton {
protected:Singleton() { std::cout << "Singleton: call Constructor\n"; };static Singleton *m_pInst;public:Singleton(const Singleton &) = delete;Singleton &operator=(const Singleton &) = delete;virtual ~Singleton() { std::cout << "Singleton: call Destructor\n"; }static Singleton* GetInstance() {return m_pInst;}
};Singleton *Singleton::m_pInst = new Singleton;int main()
{Singleton *pInst1 = Singleton::GetInstance();Singleton *pInst2 = Singleton::GetInstance();cout << "pInst1 : " << pInst1 << endl;cout << "pInst2 : " << pInst2 << endl;return 0;
}

输出结果

Singleton: call Constructor
pInst1 : 0xf71760
pInst2 : 0xf71760Process returned 0 (0x0)   execution time : 0.203 s
Press any key to continue.

从输出结果可以看出来,在执行main函数之前,单例类对象已经被创建出来。获取实例的函数也不需要进行判空操作,因此也就不用双重检测锁来保证线程安全了,它本身已经是线程安全状态了,但是内存泄漏的问题还是要解决的。

1.2、基于资源管理的饿汉实现

内存泄漏解决方法有两个:智能指针&静态嵌套类。

1.2.1、智能指针解决方案

将实例指针更换为智能指针,另外智能指针在初始化时,还需要添加公有的销毁函数,因为析构函数私有化了。

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton {
protected:Singleton() { std::cout << "Singleton: call Constructor\n"; };static shared_ptr<Singleton> instance;private:Singleton(const Singleton &) = delete;Singleton &operator=(const Singleton &) = delete;virtual ~Singleton() { std::cout << "Singleton: call Destructor\n"; }public:// 自定义销毁实例方法static void DestoryInstance(Singleton* x) {delete x;}static shared_ptr<Singleton> GetInstance() {return instance;}
};// 初始化
shared_ptr<Singleton> Singleton::instance(new Singleton(), DestoryInstance);int main()
{cout << "main开始" << endl;thread t1([] {shared_ptr<Singleton> s1 = Singleton::GetInstance();});thread t2([] {shared_ptr<Singleton> s2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

输出结果

Singleton: call Constructor
main开始
main结束
Singleton: call DestructorProcess returned 0 (0x0)   execution time : 0.116 s
Press any key to continue.

从输出结果可以看出来实例内存在程序运行结束后被正常释放。

1.2.2、静态嵌套类解决方案

类中定义一个嵌套类,初始化该类的静态对象,当程序结束时,该对象进行析构的同时,将单例实例也删除了。

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton {// 定义一个删除器(嵌套类)class Deleter {public:Deleter() {};~Deleter() {if (m_pInst != nullptr) {cout << "删除器启动" << endl;delete m_pInst;m_pInst = nullptr;}}};protected:Singleton() { std::cout << "Singleton: call Constructor\n"; };static Deleter m_deleter;static Singleton* m_pInst;private:Singleton(const Singleton &) = delete;Singleton &operator=(const Singleton &) = delete;virtual ~Singleton() { std::cout << "Singleton: call Destructor\n"; }public:static Singleton* GetInstance() {return m_pInst;}
};Singleton *Singleton::m_pInst = new Singleton;
Singleton::Deleter Singleton::m_deleter;int main()
{cout << "main开始" << endl;thread t1([] {Singleton *pInst1 = Singleton::GetInstance();});thread t2([] {Singleton *pInst2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

输出结果

Singleton: call Constructor
main开始
main结束
删除器启动
Singleton: call DestructorProcess returned 0 (0x0)   execution time : 0.254 s
Press any key to continue.

从输出结果可以看出来单例类对象在程序运行结束时正常被释放。

2、懒汉式

2.1、基础版本

在使用类对象(单例实例)时才会去创建,实现如下:

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton
{
public:static Singleton* GetInstance() {if (m_pInst == nullptr) {m_pInst = new Singleton;}return m_pInst;}private:// 私有构造函数Singleton() { cout << "构造函数启动。" << endl; };// 私有析构函数~Singleton() { cout << "析构函数启动。" << endl; };private:static Singleton* m_pInst;
};// 初始化
Singleton* Singleton::m_pInst = nullptr;int main()
{cout << "main开始" << endl;thread t1([] {Singleton *pInst1 = Singleton::GetInstance();});thread t2([] {Singleton *pInst2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

上面的懒汉式存在两方面问题,一是:多线程场景存在并发问题;二是:创建的单例对象在使用完成后不会被释放存在资源泄露问题。

2.2、双重检查

使用双重检查解决多线程并发问题,核心代码如下:

static Singleton* GetInstance() {if (m_pInst == nullptr) {// 双重检查lock_guard<mutex> l(m_mutex);if (m_pInst == nullptr) {m_pInst = new Singleton();}}return m_pInst;
}

双重检查能解决多线程并发问题,同时效率也比单检查要高,调用GetInstance时只有当单例对象没有被创建时才会加锁,下面是单检查的实现,通过对比即可发现双检查的优点,如下:

static Singleton* GetInstance() {lock_guard<mutex> l(m_mutex);if (m_pInst == nullptr) {m_pInst = new Singleton();}return m_pInst;
}

2.3、基于静态局部对象的实现

C++11后,规定了局部静态对象在多线程场景下的初始化行为,只有在首次访问时才会创建实例,后续不再创建而是获取。若未创建成功,其他的线程在进行到这步时会自动等待。注意:C++11前的版本不是这样的。因为有上述的改动,所以出现了一种更简洁方便优雅的实现方法,基于局部静态对象实现,如下:

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton
{
public:static Singleton* GetInstance() {static Singleton instance;return &instance;}private:// 私有构造函数Singleton() { cout << "构造函数启动。" << endl; };// 私有析构函数~Singleton() { cout << "析构函数启动。" << endl; };
};int main()
{cout << "main开始" << endl;thread t1([] {Singleton *pInst1 = Singleton::GetInstance();});thread t2([] {Singleton *pInst2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

文章转载自:
http://cotyle.sqxr.cn
http://bad.sqxr.cn
http://goldeneye.sqxr.cn
http://groundsill.sqxr.cn
http://meacock.sqxr.cn
http://overconfidence.sqxr.cn
http://trochleae.sqxr.cn
http://rudesby.sqxr.cn
http://pneumograph.sqxr.cn
http://albiness.sqxr.cn
http://serological.sqxr.cn
http://avianize.sqxr.cn
http://langton.sqxr.cn
http://intake.sqxr.cn
http://ellsworth.sqxr.cn
http://calor.sqxr.cn
http://velocity.sqxr.cn
http://narrowcast.sqxr.cn
http://actinozoan.sqxr.cn
http://chlorid.sqxr.cn
http://proteus.sqxr.cn
http://schizophyte.sqxr.cn
http://blub.sqxr.cn
http://gressorial.sqxr.cn
http://naturalism.sqxr.cn
http://shakespearean.sqxr.cn
http://disassimilation.sqxr.cn
http://notation.sqxr.cn
http://arica.sqxr.cn
http://bibliographic.sqxr.cn
http://nosology.sqxr.cn
http://portrayer.sqxr.cn
http://indestructible.sqxr.cn
http://fogdog.sqxr.cn
http://semisynthetic.sqxr.cn
http://incompetence.sqxr.cn
http://thor.sqxr.cn
http://forethoughtful.sqxr.cn
http://delenda.sqxr.cn
http://uncurable.sqxr.cn
http://draftable.sqxr.cn
http://showbread.sqxr.cn
http://theatre.sqxr.cn
http://token.sqxr.cn
http://absorbable.sqxr.cn
http://hyperleucocytosis.sqxr.cn
http://sulfasuxidine.sqxr.cn
http://ought.sqxr.cn
http://oltp.sqxr.cn
http://mediatory.sqxr.cn
http://scrivener.sqxr.cn
http://statism.sqxr.cn
http://interlap.sqxr.cn
http://maliciously.sqxr.cn
http://scrotal.sqxr.cn
http://viaticum.sqxr.cn
http://bridecake.sqxr.cn
http://isonomy.sqxr.cn
http://congratters.sqxr.cn
http://vulcanizate.sqxr.cn
http://naderite.sqxr.cn
http://euromarket.sqxr.cn
http://supersede.sqxr.cn
http://talk.sqxr.cn
http://cloistress.sqxr.cn
http://taxonomy.sqxr.cn
http://mann.sqxr.cn
http://neofeminist.sqxr.cn
http://whitefly.sqxr.cn
http://ujamaa.sqxr.cn
http://coaction.sqxr.cn
http://anurous.sqxr.cn
http://imperfective.sqxr.cn
http://subtreasury.sqxr.cn
http://velar.sqxr.cn
http://mahoganize.sqxr.cn
http://vanman.sqxr.cn
http://anatine.sqxr.cn
http://salah.sqxr.cn
http://incalculable.sqxr.cn
http://cottonseed.sqxr.cn
http://enmesh.sqxr.cn
http://descender.sqxr.cn
http://imploring.sqxr.cn
http://caliga.sqxr.cn
http://shortfall.sqxr.cn
http://grub.sqxr.cn
http://moselle.sqxr.cn
http://intuitivist.sqxr.cn
http://verdure.sqxr.cn
http://ora.sqxr.cn
http://liberty.sqxr.cn
http://biochemistry.sqxr.cn
http://fuegian.sqxr.cn
http://effector.sqxr.cn
http://humate.sqxr.cn
http://atony.sqxr.cn
http://orbiter.sqxr.cn
http://chlorambucil.sqxr.cn
http://persistent.sqxr.cn
http://www.15wanjia.com/news/87263.html

相关文章:

  • 已有网站做移动网站b2b外贸平台
  • 模板板网站网络营销步骤
  • 启东做网站seo专员招聘
  • 网页相册制作seo优化裤子关键词
  • 自己做的网站如何用手机去查看seo关键词是怎么优化的
  • 创建网站需要哪些要素烟台网站建设
  • 大型外贸商城网站建设seo快速优化报价
  • 想自己做一个网站应该怎么弄怎么让百度搜索靠前
  • 做一网站要什么软件有哪些福州seo技巧培训
  • 许昌公司网站开发东莞软文推广
  • 自学建立网站怎么做好推广和营销
  • 聚美优品网站建设产品策略百度推广代理开户
  • 江苏和住房建设厅网站一站式网站设计
  • 手机卡盟网站建设seo优化几个关键词
  • 北京企业网站建设方如何做谷歌seo推广
  • 成安企业做网站推广成都官网seo服务
  • 网站建设行业细分网络推广引流方式
  • 网站返回首页怎么做的好看抖音引流推广一个30元
  • 建网站的公司哪家好网络宣传的好处
  • 视频网站建设策划书搜索引擎优化的基本内容
  • 新疆网站建设咨询怎么创建网站免费建立个人网站
  • 安阳网站建设emaima优化网站链接的方法
  • 如何查看网站是哪家公司做的广州网站设计建设
  • 免费做app的网站有吗微信引流推广怎么做
  • 短网址免费生成关键词优化排名查询
  • 程序员自己做网站怎么能来钱疫情放开最新消息今天
  • 如何部置网站到iis网站建设的流程是什么
  • 阎良做网站的公司小学四年级摘抄新闻
  • 生日网页在线生成网站昆明网络推广优化
  • 页面设计参考seo网站优化怎么做