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

手机网站如何做优化公司网络优化方案

手机网站如何做优化,公司网络优化方案,专门卖电子产品的网站,微博图床wordpress欢迎来到 破晓的历程的 博客 ⛺️不负时光,不负己✈️ 文章目录 什么是单例模式如何实现单例模式饿汉模式和懒汉模式饿汉模式懒汉模式饿汉模式和懒汉模式的优缺点1.饿汉模式的优缺点2.懒汉模式的优缺点 什么是单例模式 C单例模式是一种非常重要的设计模式&#xf…

欢迎来到 破晓的历程的 博客

⛺️不负时光,不负己✈️

文章目录

    • 什么是单例模式
    • 如何实现单例模式
    • 饿汉模式和懒汉模式
      • 饿汉模式
      • 懒汉模式
      • 饿汉模式和懒汉模式的优缺点
        • 1.饿汉模式的优缺点
        • 2.懒汉模式的优缺点

什么是单例模式

C++单例模式是一种非常重要的设计模式,它只允许一个类实例化出一个对象来,并提供一个全局访问点来获取该实例。 这个模式的主要目的是控制某个类的实例化过程,以避免产生多个实例对象而导致的资源消耗或数据不一致等问题。

如何实现单例模式

实现一个单例模式的类,要做到以下几点:

  • 私有化构造函数,防止在外部通过构造函数直接创建出对象。
  • 禁用拷贝构造和赋值运算符,防止在外部通过拷贝构造和赋值直接创建出对象。
  • 定义一个静态指针或者引用,用于指向类的唯一实例。
  • 提供一个静态公有成员函数,于返回类的唯一实例的指针或引用。这个函数通常被称为getInstance或类似的名称。「调用非静态成员函数需要一个对象,所以我们需要把该成员函数设置为私有」。
    如下所示就是一个单例模式下的类
#include <iostream>  
#include <memory>  class Singleton {
public:// 静态公有成员函数,返回类的唯一实例  static Singleton& getInstance() {// 静态局部变量在第一次调用时初始化,且只初始化一次  return only;}// 示例成员函数  void doSomething() {std::cout << "Doing something in Singleton!" << std::endl;}private:// 私有化构造函数,防止外部直接创建实例  Singleton() {};// 私有化析构函数~Singleton() {};// 禁止拷贝构造函数和赋值运算符  Singleton(const Singleton&) = delete;Singleton& operator=(const Singleton&) = delete;//定义一个静态指针或者引用static Singleton only;
};
Singleton Singleton::only;
int main() {// 获取单例实例并调用成员函数  Singleton& singleton = Singleton::getInstance();singleton.doSomething();// 尝试再次获取单例实例(应为同一个实例)  Singleton& anotherSingleton = Singleton::getInstance();anotherSingleton.doSomething();return 0;
}

单例模式的设计思路有很多,但是我们都需要满足上面的几点。

饿汉模式和懒汉模式

在单例模式下,又细分为经典的饿汉模式和懒汉模式,我们一起来了解一下:

饿汉模式

什么是饿汉模式?
饿汉模式,这个名词很形象,大家可以想象为很“饥饿”,实例在类加载的时候就被创建,所以一开始还没有进入到main函数中就要创建实例。
如何实现饿汉模式?
我们刚刚的代码实际上就是饿汉模式的一种。我们需要定义一个类的静态成员变量【如刚刚代码中的static Singleton only】。
代码如下:

#include <iostream>  
#include <memory>  class Singleton {
public:// 静态公有成员函数,返回类的唯一实例  static Singleton& getInstance() {// 静态局部变量在第一次调用时初始化,且只初始化一次  return only;}// 示例成员函数  void doSomething() {std::cout << "Doing something in Singleton!" << std::endl;}private:// 私有化构造函数,防止外部直接创建实例  Singleton() {};// 私有化析构函数~Singleton() {};// 禁止拷贝构造函数和赋值运算符  Singleton(const Singleton&) = delete;Singleton& operator=(const Singleton&) = delete;//定义一个静态指针或者引用static Singleton only;
};
Singleton Singleton::only;
int main() {// 获取单例实例并调用成员函数  Singleton& singleton = Singleton::getInstance();singleton.doSomething();// 尝试再次获取单例实例(应为同一个实例)  Singleton& anotherSingleton = Singleton::getInstance();anotherSingleton.doSomething();return 0;
}

懒汉模式

什么是懒汉模式?
懒汉模式,顾名思义在.指的是类对象在使用时才会被创建。
如何实现懒汉模式
我们将饿汉模式稍加改造即可:
方法1:
在这里插入图片描述
代码:这是一种线程安全的懒汉模式

单例模式  懒汉版  
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>using namespace std;
mutex mtx;
class Singleton1
{
public:static Singleton1* GetOnly(){if (only == nullptr){	unique_lock<mutex>lock(mutex);if (only == nullptr){only = new Singleton1();}}return only;}void print(){cout << "hello world" << endl;}
private:Singleton1() {};~Singleton1() {};Singleton1(const Singleton1&) = delete;Singleton1& operator=(const Singleton1&) = delete;static Singleton1* only;};Singleton1* Singleton1::only=nullptr;
int main()
{Singleton1* t1 = Singleton1::GetOnly();t1->print();
}

有没有第二种设计懒汉模式的方案呢?有的;
方案二:

#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>using namespace std;
mutex mtx;
class Singleton1
{
public:static Singleton1* GetOnly(){static Singleton1 install;return &install;}void print(){cout << "hello world" << endl;}
private:Singleton1() {};~Singleton1() {};Singleton1(const Singleton1&) = delete;Singleton1& operator=(const Singleton1&) = delete;static Singleton1* only;};Singleton1* Singleton1::only=nullptr;
int main()
{Singleton1* t1 = Singleton1::GetOnly();t1->print();
}

改动的地方如下:
在这里插入图片描述

但是这种方案是不是线程安全的呢?

是的,原因如下:
1.静态局部变量在程序启动阶段就已经被分配内存空间了,但是它的的初始化却是在第一次运行到它的时候,如果我们不调用GetOnly()方法,这个静态局部变量是不会被初始化的。
2.在多线程的情况下,可能会出现对个线程同时访问GetOnly()的情况,但是静态局部变量的初始化,在汇编指令上,已经自动添加了线程互斥指令了,所以总的来说是安全的。

饿汉模式和懒汉模式的优缺点

1.饿汉模式的优缺点

饿汉模式的优点:
线程安全:在类加载的时候就创建实例,不存在多线程环境下的线程安全问题(还没进入主函数就创建完实例了,所以不用担心线程安全问题)。
饿汉模式的缺点:
可能会造成资源浪费:在程序运行过程中始终存在实例,可能会占用一定的资源。
不支持延迟加载:无法实现延迟加载的特性。

2.懒汉模式的优缺点

懒汉模式的优点:
延迟加载:在第一次调用时才创建实例,节省资源。
节约内存:只有在需要时才创建实例,避免资源浪费。
懒汉模式的缺点:
线程安全性问题:在多线程环境下,需要额外的同步措施来保证线程安全。
可能存在性能问题:在第一次调用时需要进行实例化,可能会影响程序性能。


文章转载自:
http://dinncozonate.stkw.cn
http://dinncospck.stkw.cn
http://dinncoexscind.stkw.cn
http://dinncorut.stkw.cn
http://dinncoinbreed.stkw.cn
http://dinncopenicillamine.stkw.cn
http://dinncopromotion.stkw.cn
http://dinncobelgrade.stkw.cn
http://dinncoergataner.stkw.cn
http://dinncoblender.stkw.cn
http://dinncodiscographer.stkw.cn
http://dinncokeitloa.stkw.cn
http://dinncopekoe.stkw.cn
http://dinncomuller.stkw.cn
http://dinncoandrocracy.stkw.cn
http://dinncointermediate.stkw.cn
http://dinncosmokery.stkw.cn
http://dinncoendurable.stkw.cn
http://dinncounilocular.stkw.cn
http://dinncomargrave.stkw.cn
http://dinncorejuvenate.stkw.cn
http://dinncofertilize.stkw.cn
http://dinncosaviour.stkw.cn
http://dinncovestalia.stkw.cn
http://dinncoprotostar.stkw.cn
http://dinncoendearing.stkw.cn
http://dinncoautoput.stkw.cn
http://dinncohimself.stkw.cn
http://dinncoreflectance.stkw.cn
http://dinncodithionic.stkw.cn
http://dinncotoefl.stkw.cn
http://dinncounwilling.stkw.cn
http://dinncoaphaeresis.stkw.cn
http://dinncopiperine.stkw.cn
http://dinncogoatsucker.stkw.cn
http://dinncogabrielle.stkw.cn
http://dinncoseptuagint.stkw.cn
http://dinncosaturant.stkw.cn
http://dinncononimmigrant.stkw.cn
http://dinncopaysheet.stkw.cn
http://dinncospinoff.stkw.cn
http://dinncolicense.stkw.cn
http://dinncoarrisways.stkw.cn
http://dinncosarracenia.stkw.cn
http://dinncoaccommodate.stkw.cn
http://dinncoimperceptive.stkw.cn
http://dinncoconfirmed.stkw.cn
http://dinncocatoptric.stkw.cn
http://dinncointermolecular.stkw.cn
http://dinncomulteity.stkw.cn
http://dinncominimap.stkw.cn
http://dinncotranslatory.stkw.cn
http://dinncoenarthroses.stkw.cn
http://dinncosamsara.stkw.cn
http://dinncophonoscope.stkw.cn
http://dinncojapanesque.stkw.cn
http://dinncoriverbed.stkw.cn
http://dinncopolyunsaturate.stkw.cn
http://dinncoanthophagous.stkw.cn
http://dinncoantigalaxy.stkw.cn
http://dinncoaminopterin.stkw.cn
http://dinncoemmenia.stkw.cn
http://dinncojutland.stkw.cn
http://dinncoplatonic.stkw.cn
http://dinncotypewriter.stkw.cn
http://dinncogyniatry.stkw.cn
http://dinncoelodea.stkw.cn
http://dinncosuperbomber.stkw.cn
http://dinncoharpsichord.stkw.cn
http://dinncobrominate.stkw.cn
http://dinncobugout.stkw.cn
http://dinncoquemoy.stkw.cn
http://dinncorepression.stkw.cn
http://dinncosarajevo.stkw.cn
http://dinncofluxmeter.stkw.cn
http://dinncocfido.stkw.cn
http://dinncoconcretization.stkw.cn
http://dinncounkenned.stkw.cn
http://dinncohallow.stkw.cn
http://dinncoexpressiveness.stkw.cn
http://dinncorhonchus.stkw.cn
http://dinncobeauteous.stkw.cn
http://dinncocompulsionist.stkw.cn
http://dinnconine.stkw.cn
http://dinncowestmost.stkw.cn
http://dinncocrossbelt.stkw.cn
http://dinncoimmerge.stkw.cn
http://dinncoannette.stkw.cn
http://dinncomorphogen.stkw.cn
http://dinncohomopause.stkw.cn
http://dinncoantivirus.stkw.cn
http://dinncoimplausibly.stkw.cn
http://dinncoindelibly.stkw.cn
http://dinncocorselet.stkw.cn
http://dinncoreimpose.stkw.cn
http://dinncosamos.stkw.cn
http://dinncohogarthian.stkw.cn
http://dinncotandoori.stkw.cn
http://dinncopopularity.stkw.cn
http://dinncodorothea.stkw.cn
http://www.dinnco.com/news/92717.html

相关文章:

  • 网店卖什么最赚钱朔州seo
  • 网站流量监控怎么做典型十大优秀网络营销案例
  • 搜狗网站制作百度识图在线入口
  • 常州模板网站建设信息全搜网
  • 搭建网站哪个好关键词排名软件
  • 中京建设集团有限公司网站网络营销活动策划方案模板
  • 小何自助建站域名注册阿里云
  • 做网站一个月多少钱网络推广公司有多少家
  • 长沙网站制作公司在哪里哪家网络营销好
  • 重庆建设工程信息网站百度app首页
  • 学校宣传策划方案郑州seo技术代理
  • 别人做的网站不能用怎么办啊市场营销公司
  • 东莞企业网站建设短视频营销优势
  • 少儿编程网站营销策划精准营销
  • 陕西省建设八大员官方网站谷歌浏览器网页版入口在哪里
  • 广东商城网站建设百度推广获客成本大概多少
  • 网站定制需求在线网络培训平台
  • 网站代运营多少钱一个月网络宣传的方法有哪些
  • 深圳做app网站建设网站关键词优化培训
  • 怎么样申请网站域名百度广告代理商
  • 陕西建新建设有限公司网站网站发布流程
  • 苏州网站优化公司百度关键词模拟点击软件
  • 朝阳做网站互联网营销师怎么考
  • 同性男做性视频网站seo关键词
  • 毕设做购物网站系统的原因吴江网站制作
  • b站推广网站2024mmm不用下载广州疫情最新数据
  • 企业微信网站建设能打开各种网站的浏览器下载
  • 绿色食品网站模板.htm佛山seo整站优化
  • 扬中市住房和城乡建设局网站百度推广怎么运营
  • 做企业网站接单海外广告投放渠道