JAVA设计模式之创造型模式介绍.docx

上传人:you****now 文档编号:61490723 上传时间:2022-11-21 格式:DOCX 页数:10 大小:38.97KB
返回 下载 相关 举报
JAVA设计模式之创造型模式介绍.docx_第1页
第1页 / 共10页
JAVA设计模式之创造型模式介绍.docx_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《JAVA设计模式之创造型模式介绍.docx》由会员分享,可在线阅读,更多相关《JAVA设计模式之创造型模式介绍.docx(10页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、JAVAA设计模模式之创创造型模模式创造型模模式包括括工厂方方法、抽抽象工厂厂、创造造者模式式、单态态模式、原型模模式共5类1、工厂厂方法定义一个个用于创创建对象象的接口口,让子子类决定定实例化化哪一个个类。FacctorryMeethood使一个个类的实实例化延迟到到其子类类。适用条件件:1)当一一个类不不知道它它所必须须创建的的对象的的类的时时候。2)当一一个类希希望由它它的子类类来指定定它所创创建的对对象的时时候。3)当类类将创建建对象的的职责委委托给多个帮助助子类中的的某一个个,并且且类希望将哪哪一个帮帮助子类类是代理理者这一一信息局局部化的的时候。参与者:1)Prroduuct:定义

2、工工厂方法法所创建建的对象象的接口口。2)CooncrreteeProoduct:实现Prooducct接口。3)Crreattor:声明工工厂方法法,该方方法返回回一个Prooduct类型的的对象。Creeatoor也可以以定义一一个工厂厂方法的的缺省实实现,它它返回一一个缺省省的ConncreetePProdductt对象。可以调调用工厂厂方法以以创建一一个Prooducct对象。4)CooncrreteeCreeatoor:重定义义工厂方方法以返返回一个个ConncreetePProdduct实例。示例:packkagee zaantiip.mmoduuless;publliccclas

3、ss FFacttoryyMetthoddTesst publlicsstatticvvoidd maain(Strringg arggs) IWorrkFaactoory stuudenntWoorkFFacttoryy = neww SttudeentWWorkkFacctorry();studdenttWorrkFaactoory.gettWorrk().dooWorrk();IWorrkFaactoory teaacheerWoorkFFacttoryy = new TeeachherWWorkkFacctorry();teaccherrWorrkFaactoory.gettWorrk

4、().dooWorrk();/ PProdducttinteerfaace Worrk voidd dooWorrk();/ CConccrettePrroduuctclasss SStuddenttWorrk iimpllemeentss Woork Oveerriidepubllicvvoidd dooWorrk() Systtem.outt.prrinttln(学生做做作业);classs TeeachherWWorkk immpleemennts Woork Oveerriidepubllicvvoidd dooWorrk() Systtem.outt.priintlln(老师审审批作

5、业业);/ CCreaatorrinteerfaace IWoorkFFacttoryy Workk geetWoork();/ CConccretteCrreattorclasss SStuddenttWorrkFaactoory impplemmentts IIWorrkFaactoory Oveerriidepubllic Worrk ggetWWorkk() retuurnnnew StuudenntWoork();classs TTeaccherrWorrkFaactoory impplemmenttsIWWorkkFacctorry Oveerriidepubllic Worrk

6、ggetWWorkk() retuurnnnew TeaacheerWoork();输出结果果:学生做作作业老师审批批作业2抽象工工厂提供一个个创建一一系列相相关或相相互依赖赖对象的的接口,而无需需指定它它们具体体的类。适用条件件:1)一个个系统要要独立于于它的产产品的创创建、组组合和表表示时。2)一个个系统要要由多个个产品系系列中的的一个来来配置时时。3)当你你要强调调一系列列相关的的产品对对象的设设计以便便进行联联合使用用时。4)当你你提供一一个产品品类库,而只想想显示它它们的接口而不不是实现现时。参与者:1)AbbstraactFFacttoryy:声明一一个创建建抽象产产品对象象的操作

7、作接口。2)CooncrreteeFacctorry:实现创创建具体体产品对对象的操操作。3)AbbstrracttProoducct:为一类类产品对对象声明明一个接接口。4)CooncrreteeProoducct:定义一一个将被被相应的的具体工工厂创建建的产品品对象,实现AbsttracctPrroduuct接口。5)Cllientt:仅使用用由AbsstraactFFacttoryy和AbsstraactPProdduc*类声明明的接口口。示例:packkagee zaantiip.mmoduuless;publliccclasss AAbsttracctFaactooryTTestt

8、publlicsstatticvvoidd maain(Strringg arggs) / cclieentIAniimallFacctorry bblacckAnnimaalFaactoory = nnew BlaackAAnimmalFFacttoryy();ICatt bllackkCatt = blacckAnnimalFFacttoryy.crreatteCaat();blacckCaat.eeat();IDogg bllackkDogg = blaackAAnimmalFFacttoryy.crreatteDoog();blacckDoog.eeat();IAniimallFacc

9、torry wwhitteAnnimaalFaactoory = nnew WhiiteAAnimmalFFacttoryy();ICatt whhiteeCatt = whiiteAAnimmalFFacttoryy.crreatteCaat();whitteCaat.eeat();IDogg whhiteeDog = whhiteeAniimallFacctorry.ccreaateDDog();whitteDoog.eeat();/ aabsttracct ffacttoryyinteerfaace IAnnimaalFaactoory ICatt crreatteCaat();IDog

10、g crreatteDoog();/ aabsttracct pprodducttinteerfaace ICaat voidd eaat();inteerfaace IDoog voidd eaat();/ cconccrette pprodducttclasss BBlacckCaat iimpllemeentss ICCat Oveerriidepubllicvvoidd eaat() Systtem.out.pprinntlnn(tthe blaack catt iss eaatinng);classs WWhitteCaat iimpllemeentss ICCat Oveerrii

11、depubllicvvoidd eaat() Systtem.outt.prrinttln(thhe wwhitte ccat is eattingg);classs BBlacckDoog iimpleemennts IDoog Oveerriidepubllicvvoidd eaat() Systtem.outt.prrinttln(thhe bblacck ddog is eattingg);classs WWhitteDoog iimpllemeentss IDDog Oveerriidepubllicvvoidd eaat() Systtem.outt.prrinttln(thhe

12、wwhitte ddog is eattingg);/ cconccreaate facctorryclasss BBlacckAnimmalFFacttoryy immpleemennts IAnnimaalFaactoory Oveerriidepubllic ICaat ccreaateCCat() retuurnnnew BlaackCCat();Oveerriidepubllic IDoog ccreaateDDog() retuurnnnew BlaackDDog();classs WWhitteAnnimaalFaactoory impplemmentts IIAniimallF

13、acctorry Oveerriidepubllic ICaat ccreatteCaat() retuurnnnew WhiiteCCat();Oveerriidepubllic IDoog ccreaateDDog() retuurnnnew WhiiteDDog();输出结果果:the blaack catt iss eaatinngthe blaack dogg iss eaatinngthe whiite catt iss eaatinngthe whiite dogg iss eaatinng3、创造造者模式式将一个复复杂对象象的构造造与它的的表示分分离,使使得同样样的构建建过程可

14、可以创建建不同的的表示。适用条件件:1)当创创建复杂杂对象的的算法应应该独立立于该对对象的组组成部分分以及它它们的装装配方式式时。2)当构构造过程程必须允允许被构造造的对象象有不同同的表示时时。参与者:1)Buuildder:为创创建一个个Buiildeer对象的的各个部部件指定定抽象接接口。2)CooncrreteeBuiildeer:实现Buiildeer的接口口以构造造和装配配该产品品的各个个部件。定义并并明确它它所创建建的表示示。提供供一个检检索产品品的接口口。3)Diirecctorr:构造造一个使使用Buiildeer接口的的对象。4)Prroduuct:表示示被构造造的复杂杂对象

15、。ConncetteBuuildder创建该该产品的的内部表表示并定定义它的的装配过过程。包包含定义义组成部部件的类类,包括括将这些些部件装装配成最最终产品品的接口口。示例:packkagee zaantiip.mmoduuless;publliccclasss CCreaatorrTesst /测试试publlicsstatticvvoidd maain(Strringg arggs) PerssonDDireectoor ppd=nnew PerrsonnDirrecttor();Persson perrsonn=pdd.coonsttrucctPeersoon(nnew MannBuii

16、ldeer();Systtem.outt.prrinttln(perrsonn.geetBoody();Systtem.outt.prrinttln(perrsonn.geetHeead();Systtem.outt.prrinttln(perrsonn.geetFooot();/ pprodducttclasss PPersson privvatee Sttrinng hheadd;privvatee Sttrinng bbodyy;privvatee Sttrinng ffoott;publlic Strringg geetHeead() retuurnhheadd;publlicvvoi

17、dd seetHeead(Strringg heead) thiss.heaad = heead;publlic Strringg geetBoody() retuurnbbodyy;publlicvooid settBoddy(SStriing boddy) thiss.boddy = boody;publlic Strringg geetFooot() retuurnffoott;publlicvvoidd seetFooot(Strringg fooot) thiss.fooot = fooot;classs MMan exttendds PPersson/ BBuillderrinte

18、erfaace PerrsonnBuiildeer voidd buuilddHeaad();voidd buiildBBodyy();voidd buuilddFooot();Persson buiildPPersson();/ cconccrette bbuillderrclasss MManBBuillderr immpleemennts PerrsonnBuiildeerPersson perrsonn;publlic MannBuiildeer()persson=neww Maan();publlicvvoidd buuilddBoddy()persson.settBoddy(创造男

19、男人的身身体);publlicvvoidd buuilddFooot()persson.settFooot(创造男男人的脚脚);publlicvvoidd buuilddHeaad()persson.settHeaad(创造男男人的头头);publlic Perrsonn buuilddPerrsonn()retuurnppersson;/Diircttorclasss PPerssonDDireectoorpubllic Perrsonn coonsttrucctPeersoon(PPerssonBBuillderr pbb)pb.bbuilldHeead();pb.bbuilldBoody

20、();pb.bbuilldFooot();retuurn pb.buiildPPersson();输出结果果:创造男人人的身体体创造男人人的头创造男人人的脚4、单态态模式保证一个个类仅有有一个实实例,仅仅提供一一个访问问他的全全局访问问点。适用条件件:1)当类类只能有有一个实实例而且且客户可可以从一一个总所所周知的的访问点点访问它它时。2)当这这个唯一一实例应应该是通通过子类类化可扩扩展的,并且客客户应该该无需更更改代码码就能使使用一个个扩展的的实例时时。参与者:Singgletton:定义一一个Insstannce操作,允许客客户访问问它的唯唯一实例例。Insstannce是一个个类操作作。

21、可能能负责创建它它自己的的唯一实实例。示例:packkagee zaantiip.mmoduuless;publliccclasss SSingglettonTTestt /测试试publlicsstatticvvoidd maain(Strringg arggs) Singgletton sinng = Siinglletoon.ggetIInsttancce();Singgletton sinng2 = SSinggletton.gettInsstannce();Systtem.outt.prrinttln(sinng);Systtem.outt.prrinttln(sinng2);cla

22、sss SSinggletton privvateestaaticc Siinglletoon ssingg;privvatee Siinglletoon() Systtem.outt.prrinttln(单实例例模式对对象);publlicsstattic Sinngleetonn geetInnstaancee() if (sinng = nnulll) singg = new SSinggletton();retuurnssingg;输出结果果:单实例模模式对象象zanttip.moddulees.SSingglettonde66ceddzanttip.moddulees.SSinggl

23、ettonde66cedd5、原型型模式用原型实实例指定定创建对对象的种种类,并并且通过过拷贝这这些原型型创建新新的对象象。适用条件件:1)当一一个系统统应该独独立于它它的产品品创建、构成成和表示示时。2)当要要实例化化的类是是在运行行时刻指指定时,例如,通过动动态装载载。3)为了了避免创创建一个个与产品品类层次次平行的的工厂层层次时。4)当一一个类的的实例只只能有几几个不同同状态组组合中的的一种时时。建立立相应数数目的原原型并克克隆它们们可能比比每次用用合适的的状态手手工实例例化该类类更方便便一些。参与者:1)Prrotootyppe:声明一一个克隆隆自身的的接口。2)Cooncrretee

24、Prootottypee:实现一一个克隆隆自身的的操作。3)Clliennt:让一个个原型克克隆自身身从而创创建一个个新的对对象。示例:packkagee zaantiip.mmoduuless;publliccclasss PProttotyypeTTestt /测试试publlicsstatticvvoidd maain(Strringg arggs) throows Exccepttionn Prottotyype prootyppe = neew CConccrettePrrotootyppe(北京);Prottotyype prootyppe2 = (Prootottypee) pp

25、rottypee.cllonee();Systtem.outt.prrinttln(prootyppe);Systtem.outt.prrinttln(prootyppe.ggetNNamee();Systtem.outt.prrinttln(prootyppe2);Systtem.outt.prrinttln(prootyppe2.gettNamme();/ pprottotyypeclasss PProttotyype impplemmentts CClonneabble privvatee Sttrinng nnamee;publlic Strringg geetNaame() retu

26、urnnnamee;publlicvvoidd seetNaame(Strringg naame) thiss.namme = naame;Oveerriideprottectted Objjectt cllonee() thrrowss ClloneeNottSuppporrteddExccepttionn try retuurnssupeer.cclonne(); caatchh (EExceeptiion ex) ex.pprinntSttackkTraace();retuurnnnulll;/ CConccrettePrrotootyppeclasss CConccrettePrrotootyppe eexteendss Prrotootyppe publlic CConccrettePrrotootyppe(SStriing namme) setNNamee(naame);输出结果果:zanttip.moddulees.CConccrettePrrotootyppecc171164北京zanttip.moddulees.CConccrettePrrotootyppe11fb88ee33北京

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文献 > 管理手册

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com