软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt

软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt

ID:51621354

大小:263.50 KB

页数:43页

时间:2020-03-26

软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt_第1页
软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt_第2页
软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt_第3页
软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt_第4页
软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt_第5页
资源描述:

《软件设计模式-体系结构-PPT讲稿 0-Software-Design-Patterns-20121030 Lec3 Singleton Pattern-Creational.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、Lecture3SingletonPattern(Creational)单例模式Yushan(Michael)Sun2012-10-23Contents1)TheConceptoftheSingletonPattern2)ExampleofUsingtheSingletonPattern3)TheSingletonPatterninMulti-threadProgrammingSingletonPattern(Creational)单例模式TheConceptoftheSingletonPatternTheConceptoftheSingletonPat

2、ternReallifeexamples:OnecountryhasonlyonepresidentAcompanyhasonlyonegeneralmanagerAhusbandhasonlyonewifeOnebankaccounthasonlyonecreditcard现实世界的单例的例子软件中的单例情况Insoftwaredesign,someclassesmayonlyhaveoneinstance,thatis,onecancreateonlyoneobjectforoneclassandonecannotcreatetwoobjects.A

3、systemmayhaveonlyoneglobalobjecttocoordinatethewholebehaviorofthesystem.Example:Inaserverprogram,theconfigurationinformationisputintoafile,whichisreadbyasingletonobject.OtherobjectscanusethissingletonobjecttogettheconfigurationinformationAcompanycanhavemanyprinters,butprintingman

4、agementsystemhasonlyoneprinttaskcontrolobject,whichmanagesprintqueueanddistributestaskstoprintersTheWindowssystemhasonlyonefilesystemandawindowmanager.TheConceptoftheSingletonPattern怎样确保某个类仅仅能生成一个唯一的实例?Therearetwowaystoguaranteethatoneclasshasonlyoneinstanceandthisinstancecanbeea

5、silyobtained方法1:Onewayisthataprogrammerusescodeinaprogramtomakesurethatonlyoneobjectofaparticularclassbecreated;方法2:TheothermethodiscarefullydesignthestructureofaclassthatallowsonlyoneuniqueinstanceiscreatedtomakesureonlyoneobjectcanbecreatedTheConceptoftheSingletonPatternClientC

6、lassSingletonClass只能被创建一个唯一实例的类客户类方法1:在客户类中使用代码确保仅仅有一个SingletonClass的对象被创建方法2:精心设计SingletonClass类,使得仅仅唯一的一个该类的对象能被创建TheConceptoftheSingletonPattern第一种方法不好,因为程序员是很可能疏忽,而导致第二个实例被创建。第二种方法是从根本上保证仅仅有一个实例被创建的有效方法。这就是本节所讲述的实例模式所要表述的内容。TheConceptoftheSingletonPatternTheintentofthesinglet

7、onpattern:toguaranteethatoneclasshasonlyauniqueinstanceandprovideaglobalaccesspointforit.实现单例模式的思想:Inordertopreventclientprogramfromusingtheconstructortocreatemultipleobjects,theconstructorcanbeclaimedasprivatesothenoclientprogramcanuseittocreateanyobject问题:Iftheconstructorisclai

8、medasprivatetypethennoclientprogramcanus

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。