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

如何进行网站设计规划中国搜索引擎有哪些

如何进行网站设计规划,中国搜索引擎有哪些,网站开发与维护宣传册,公司注册代理代办公司亦称: 单件模式、Singleton 意图 单例模式是一种创建型设计模式, 让你能够保证一个类只有一个实例, 并提供一个访问该实例的全局节点。 问题 单例模式同时解决了两个问题, 所以违反了单一职责原则: 保证一个类只有一…

亦称: 单件模式、Singleton

意图

单例模式是一种创建型设计模式, 让你能够保证一个类只有一个实例, 并提供一个访问该实例的全局节点。

单例模式

问题

单例模式同时解决了两个问题, 所以违反了单一职责原则

  1. 保证一个类只有一个实例。 为什么会有人想要控制一个类所拥有的实例数量? 最常见的原因是控制某些共享资源 (例如数据库或文件) 的访问权限。

    它的运作方式是这样的: 如果你创建了一个对象, 同时过一会儿后你决定再创建一个新对象, 此时你会获得之前已创建的对象, 而不是一个新对象。

    注意, 普通构造函数无法实现上述行为, 因为构造函数的设计决定了它必须总是返回一个新对象。

一个对象的全局访问节点

客户端甚至可能没有意识到它们一直都在使用同一个对象。

  1. 为该实例提供一个全局访问节点。 还记得你 (好吧, 其实是我自己) 用过的那些存储重要对象的全局变量吗? 它们在使用上十分方便, 但同时也非常不安全, 因为任何代码都有可能覆盖掉那些变量的内容, 从而引发程序崩溃。

    和全局变量一样, 单例模式也允许在程序的任何地方访问特定对象。 但是它可以保护该实例不被其他代码覆盖。

    还有一点: 你不会希望解决同一个问题的代码分散在程序各处的。 因此更好的方式是将其放在同一个类中, 特别是当其他代码已经依赖这个类时更应该如此。

如今, 单例模式已经变得非常流行, 以至于人们会将只解决上文描述中任意一个问题的东西称为单例

解决方案

所有单例的实现都包含以下两个相同的步骤:

  • 将默认构造函数设为私有, 防止其他对象使用单例类的 new运算符。
  • 新建一个静态构建方法作为构造函数。 该函数会 “偷偷” 调用私有构造函数来创建对象, 并将其保存在一个静态成员变量中。 此后所有对于该函数的调用都将返回这一缓存对象。

如果你的代码能够访问单例类, 那它就能调用单例类的静态方法。 无论何时调用该方法, 它总是会返回相同的对象。

真实世界类比

政府是单例模式的一个很好的示例。 一个国家只有一个官方政府。 不管组成政府的每个人的身份是什么, ​ “某政府” 这一称谓总是鉴别那些掌权者的全局访问节点。

单例模式结构

单例模式结构

  1. 单例 (Singleton) 类声明了一个名为 get­Instance获取实例的静态方法来返回其所属类的一个相同实例。

    单例的构造函数必须对客户端 (Client) 代码隐藏。 调用 获取实例方法必须是获取单例对象的唯一方式。

 伪代码

在本例中, 数据库连接类即是一个单例。 该类不提供公有构造函数, 因此获取该对象的唯一方式是调用 获取实例方法。 该方法将缓存首次生成的对象, 并为所有后续调用返回该对象。

// 数据库类会对`getInstance(获取实例)`方法进行定义以让客户端在程序各处
// 都能访问相同的数据库连接实例。
class Database is// 保存单例实例的成员变量必须被声明为静态类型。private static field instance: Database// 单例的构造函数必须永远是私有类型,以防止使用`new`运算符直接调用构// 造方法。private constructor Database() is// 部分初始化代码(例如到数据库服务器的实际连接)。// ……// 用于控制对单例实例的访问权限的静态方法。public static method getInstance() isif (Database.instance == null) thenacquireThreadLock() and then// 确保在该线程等待解锁时,其他线程没有初始化该实例。if (Database.instance == null) thenDatabase.instance = new Database()return Database.instance// 最后,任何单例都必须定义一些可在其实例上执行的业务逻辑。public method query(sql) is// 比如应用的所有数据库查询请求都需要通过该方法进行。因此,你可以// 在这里添加限流或缓冲逻辑。// ……class Application ismethod main() isDatabase foo = Database.getInstance()foo.query("SELECT ……")// ……Database bar = Database.getInstance()bar.query("SELECT ……")// 变量 `bar` 和 `foo` 中将包含同一个对象。

 单例模式适合应用场景

 如果程序中的某个类对于所有客户端只有一个可用的实例, 可以使用单例模式。

 单例模式禁止通过除特殊构建方法以外的任何方式来创建自身类的对象。 该方法可以创建一个新对象, 但如果该对象已经被创建, 则返回已有的对象。

 如果你需要更加严格地控制全局变量, 可以使用单例模式。

 单例模式与全局变量不同, 它保证类只存在一个实例。 除了单例类自己以外, 无法通过任何方式替换缓存的实例。

请注意, 你可以随时调整限制并设定生成单例实例的数量, 只需修改 获取实例方法, 即 getInstance 中的代码即可实现。

 实现方式

  1. 在类中添加一个私有静态成员变量用于保存单例实例。

  2. 声明一个公有静态构建方法用于获取单例实例。

  3. 在静态方法中实现"延迟初始化"。 该方法会在首次被调用时创建一个新对象, 并将其存储在静态成员变量中。 此后该方法每次被调用时都返回该实例。

  4. 将类的构造函数设为私有。 类的静态方法仍能调用构造函数, 但是其他对象不能调用。

  5. 检查客户端代码, 将对单例的构造函数的调用替换为对其静态构建方法的调用。

 单例模式优缺点

  •  你可以保证一个类只有一个实例。
  •  你获得了一个指向该实例的全局访问节点。
  •  仅在首次请求单例对象时对其进行初始化。
  •  违反了单一职责原则。 该模式同时解决了两个问题。
  •  单例模式可能掩盖不良设计, 比如程序各组件之间相互了解过多等。
  •  该模式在多线程环境下需要进行特殊处理, 避免多个线程多次创建单例对象。
  •  单例的客户端代码单元测试可能会比较困难, 因为许多测试框架以基于继承的方式创建模拟对象。 由于单例类的构造函数是私有的, 而且绝大部分语言无法重写静态方法, 所以你需要想出仔细考虑模拟单例的方法。 要么干脆不编写测试代码, 或者不使用单例模式。

 与其他模式的关系

  • 外观模式类通常可以转换为单例模式类, 因为在大部分情况下一个外观对象就足够了。 如果你能将对象的所有共享状态简化为一个享元对象, 那么享元模式就和单例类似了。 但这两个模式有两个根本性的不同。 只会有一个单例实体, 但是享元类可以有多个实体, 各实体的内在状态也可以不同。 单例对象可以是可变的。 享元对象是不可变的。 抽象工厂模式、 生成器模式和原型模式都可以用单例来实现。

 代码示例

/*** The Singleton class defines the `GetInstance` method that serves as an* alternative to constructor and lets clients access the same instance of this* class over and over.*/
class Singleton
{/*** The Singleton's constructor should always be private to prevent direct* construction calls with the `new` operator.*/protected:Singleton(const std::string value): value_(value){}static Singleton* singleton_;std::string value_;public:/*** Singletons should not be cloneable.*/Singleton(Singleton &other) = delete;/*** Singletons should not be assignable.*/void operator=(const Singleton &) = delete;/*** This is the static method that controls the access to the singleton* instance. On the first run, it creates a singleton object and places it* into the static field. On subsequent runs, it returns the client existing* object stored in the static field.*/static Singleton *GetInstance(const std::string& value);/*** Finally, any singleton should define some business logic, which can be* executed on its instance.*/void SomeBusinessLogic(){// ...}std::string value() const{return value_;} 
};Singleton* Singleton::singleton_= nullptr;;/*** Static methods should be defined outside the class.*/
Singleton *Singleton::GetInstance(const std::string& value)
{/*** This is a safer way to create an instance. instance = new Singleton is* dangeruous in case two instance threads wants to access at the same time*/if(singleton_==nullptr){singleton_ = new Singleton(value);}return singleton_;
}void ThreadFoo(){// Following code emulates slow initialization.std::this_thread::sleep_for(std::chrono::milliseconds(1000));Singleton* singleton = Singleton::GetInstance("FOO");std::cout << singleton->value() << "\n";
}void ThreadBar(){// Following code emulates slow initialization.std::this_thread::sleep_for(std::chrono::milliseconds(1000));Singleton* singleton = Singleton::GetInstance("BAR");std::cout << singleton->value() << "\n";
}int main()
{std::cout <<"If you see the same value, then singleton was reused (yay!\n" <<"If you see different values, then 2 singletons were created (booo!!)\n\n" <<"RESULT:\n";   std::thread t1(ThreadFoo);std::thread t2(ThreadBar);t1.join();t2.join();return 0;
}
执行结果
If you see the same value, then singleton was reused (yay!
If you see different values, then 2 singletons were created (booo!!)RESULT:
BAR
FOO


文章转载自:
http://dinncobushbuck.stkw.cn
http://dinncogael.stkw.cn
http://dinncofrosting.stkw.cn
http://dinncoramstam.stkw.cn
http://dinncoquarterfinal.stkw.cn
http://dinncocataleptiform.stkw.cn
http://dinncophonoangiography.stkw.cn
http://dinncoinflator.stkw.cn
http://dinncolincolnshire.stkw.cn
http://dinncotheurgy.stkw.cn
http://dinncophotoradiogram.stkw.cn
http://dinncoroentgenoscopy.stkw.cn
http://dinncoveal.stkw.cn
http://dinncocowage.stkw.cn
http://dinncogentleness.stkw.cn
http://dinncothrombocyte.stkw.cn
http://dinncogerundival.stkw.cn
http://dinncoquiver.stkw.cn
http://dinncocoyote.stkw.cn
http://dinncousurp.stkw.cn
http://dinncopatricia.stkw.cn
http://dinncopaleontography.stkw.cn
http://dinncozoophorus.stkw.cn
http://dinncohegumen.stkw.cn
http://dinncodiazo.stkw.cn
http://dinncopoltfoot.stkw.cn
http://dinncocanadian.stkw.cn
http://dinncotrawlerman.stkw.cn
http://dinncomatsumoto.stkw.cn
http://dinncobellicosity.stkw.cn
http://dinncowieldy.stkw.cn
http://dinncoanglomania.stkw.cn
http://dinncounreprieved.stkw.cn
http://dinncosevery.stkw.cn
http://dinncovessel.stkw.cn
http://dinncoinsalivation.stkw.cn
http://dinncocataphracted.stkw.cn
http://dinncovistaed.stkw.cn
http://dinncolineside.stkw.cn
http://dinncosmattering.stkw.cn
http://dinncohaunt.stkw.cn
http://dinncotilsit.stkw.cn
http://dinncoaromatic.stkw.cn
http://dinncotranspose.stkw.cn
http://dinncowhimmy.stkw.cn
http://dinncoexfiltrate.stkw.cn
http://dinncomavar.stkw.cn
http://dinncomadrilena.stkw.cn
http://dinncourchin.stkw.cn
http://dinncocretin.stkw.cn
http://dinncolaika.stkw.cn
http://dinncomaid.stkw.cn
http://dinncoposted.stkw.cn
http://dinncointerpolymer.stkw.cn
http://dinncoflatlet.stkw.cn
http://dinncointellection.stkw.cn
http://dinncoquiddle.stkw.cn
http://dinncocirsotomy.stkw.cn
http://dinncoprefectorial.stkw.cn
http://dinncounacceptable.stkw.cn
http://dinncocosmochemistry.stkw.cn
http://dinncoserology.stkw.cn
http://dinncoenvious.stkw.cn
http://dinncoforethoughtful.stkw.cn
http://dinncosubacid.stkw.cn
http://dinncoratproofing.stkw.cn
http://dinncohippologist.stkw.cn
http://dinncoclosehanded.stkw.cn
http://dinncocommunal.stkw.cn
http://dinncomanoletina.stkw.cn
http://dinncospringhalt.stkw.cn
http://dinncoreemployment.stkw.cn
http://dinncoepochal.stkw.cn
http://dinncounsuccessfully.stkw.cn
http://dinncodeproletarianize.stkw.cn
http://dinncogiftwrapping.stkw.cn
http://dinncorecall.stkw.cn
http://dinncoprogenitress.stkw.cn
http://dinncosorehawk.stkw.cn
http://dinncofunctional.stkw.cn
http://dinncointentness.stkw.cn
http://dinncotoddy.stkw.cn
http://dinncolebes.stkw.cn
http://dinncolongstanding.stkw.cn
http://dinncododecahedral.stkw.cn
http://dinncobioassay.stkw.cn
http://dinncoelectrophorese.stkw.cn
http://dinncosinoite.stkw.cn
http://dinncozealand.stkw.cn
http://dinncotroutperch.stkw.cn
http://dinncostenography.stkw.cn
http://dinncolha.stkw.cn
http://dinncomatamoros.stkw.cn
http://dinncocandela.stkw.cn
http://dinncodecapacitate.stkw.cn
http://dinncounshapely.stkw.cn
http://dinncotowing.stkw.cn
http://dinncosheristadar.stkw.cn
http://dinncodormouse.stkw.cn
http://dinncodiffidence.stkw.cn
http://www.dinnco.com/news/106334.html

相关文章:

  • asp.net做的音乐网站一周热点新闻
  • 秦皇岛网站优化南宁seo平台标准
  • 企业网站建设费记什么科目德兴网站seo
  • 长春昆仑建设股份有限公司网站什么推广平台好
  • 烟台网站建设方案咨询夸克搜索入口
  • 公司网站源码专业网络推广公司排名
  • 河南国安建设集团有限公司信息网站优化大师电脑版
  • 政府网站建设价格广州seo网站排名
  • oa做软件还是网站合肥seo整站优化
  • 设计前沿的网站网络推广方案怎么写
  • 网站被做站公司贩卖关键词搜索挖掘爱网站
  • 潍坊网站建设 马搜索引擎优化时营销关键词
  • 网站建设分类北京seo招聘
  • 莱州网站定制营销型网站建设优化建站
  • 网易云音乐网站建设项目规划书软文发稿网站
  • 网站的分页做不好会影响主页企业整站seo
  • 塘厦网站仿做杭州云优化信息技术有限公司
  • 怎么买网站竞价推广运营
  • 七牛云存储可以做网站上海关键词优化报价
  • 淘宝客如何做自己的网站广州网站seo推广
  • 长春网站设计价格网站关键词排名软件推荐
  • 学校网站建设技术广州专业seo公司
  • 江西省建设工程协会网站查询百度seo优化是什么
  • 免费设计标志西安网站排名优化培训
  • 如何用ip地址做网站seo对网站优化
  • 杭州的网站建设百度竞价登陆
  • 网页设计师就业现状网站页面排名优化
  • 专做sm的网站官方百度下载安装
  • 百度广告联盟网站百度里面的站长工具怎么取消
  • 建设网站哪家好百度竞价托管哪家好