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





《JAVA设计模式之创造型模式介绍2151.docx》由会员分享,可在线阅读,更多相关《JAVA设计模式之创造型模式介绍2151.docx(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、JAVA设设计模式之之创造型模模式创造型模式式包括工厂厂方法、抽抽象工厂、创创造者模式式、单态模模式、原型型模式共5类1、工厂方方法定义一个用用于创建对对象的接口口,让子类类决定实例例化哪一个个类。FacttoryMMethood使一个类类的实例化化延迟到其其子类。适用条件:1)当一个个类不知道道它所必须须创建的对对象的类的的时候。2)当一个个类希望由由它的子类类来指定它它所创建的的对象的时时候。3)当类将将创建对象象的职责委委托给多个帮助子类中的某某一个,并并且类希望将哪一一个帮助子子类是代理理者这一信信息局部化化的时候。参与者:1)Prooductt:定义工厂厂方法所创创建的对象象的接口。
2、2)ConncretteProoduct:实现Prodduct接口。3)Creeatorr:声明工厂厂方法,该该方法返回回一个Product类型的对对象。Creaator也可以定定义一个工工厂方法的的缺省实现现,它返回回一个缺省省的ConccreteeProdduct对象。可可以调用工工厂方法以以创建一个个Prodduct对象。4)ConncretteCreeatorr:重定义工工厂方法以以返回一个个ConccreteeProdduct实例。示例:packaage zaantipp.modduless;publiic classs FaactorryMetthodTTest publlic s
3、tattic voidd maiin(Sttringg aargs) IWoorkFaactorry sttudenntWorrkFacctoryy = nnew SStudeentWoorkFaactorry();stuudenttWorkkFacttory.getWWork().dooWorkk();IWoorkFaactorry teeacheerWorrkFacctoryy = neew TeaacherrWorkkFacttory();teaacherrWorkkFacttory.getWWork().dooWorkk();/ Prroducctinterrfacee Worrk v
4、oidd doWWork();/ CooncreetePrroducctclasss StuudenttWorkk impplemeents Workk Oveerriddepubllic voidd doWWork() Sysstem.out.pprinttln(学生做作作业);classs TeaacherWoork iimpleementts Worrk Oveerriddepubllic voidd doWWork() Sysstem.out.pprinttln(老师审批批作业);/ Crreatoorinterrfacee IWoorkFaactorry Workk gettWork
5、k();/ CooncreeteCrreatoorclasss StuudenttWorkkFacttory impllemennts IIWorkkFacttory Oveerriddepubllic WWork getWWork() retturn new SStudeentWoork();classs TeaacherrWorkkFacttory impllemennts IWorrkFacctoryy Oveerriddepubllic WWork getWWork() retturn new TTeachherWoork();输出结果:学生做作业业老师审批作作业2 抽象工工厂提供一个
6、创创建一系列列相关或相相互依赖对对象的接口口,而无需需指定它们们具体的类类。适用条件:1)一个系系统要独立立于它的产产品的创建建、组合和和表示时。2)一个系系统要由多多个产品系系列中的一一个来配置置时。3)当你要要强调一系系列相关的的产品对象象的设计以以便进行联联合使用时时。4)当你提提供一个产产品类库,而而只想显示示它们的接口而不是是实现时。参与者:1)AbsstracctFacctoryy:声明一个个创建抽象象产品对象象的操作接接口。2)ConncretteFacctoryy:实现创建建具体产品品对象的操操作。3)AbsstracctProoductt:为一类产产品对象声声明一个接接口。4
7、)ConncretteProoduct:定义一个个将被相应应的具体工工厂创建的的产品对象,实现AbstrractPProduuct接口。5)Cliient:仅使用由AbsttracttFacttory和AbsttracttProdduc*类声明的的接口。示例:packaage zzantiip.moodulees;publiic classs AbbstraactFaactorryTesst publlic stattic voidd maiin(Sttringg aargs) / clieentIAnnimallFacttory blacckAniimalFFactoory = neww B
8、laackAnnimallFacttory();ICaat bllackCCat = blackkAnimmalFaactorry.crreateeCat();blaackCaat.eaat();IDoog bllackDDog = blaackAnnimallFacttory.ccreatteDogg();blaackDoog.eaat();IAnnimallFacttory whitteAniimalFFactoory = neww WhiiteAnnimallFacttory();ICaat whhiteCCat = whiiteAnnimallFacttory.creaateCaat(
9、);whiiteCaat.eaat();IDoog whhiteDDog = whitteAniimalFFactoory.ccreatteDogg();whiiteDoog.eaat();/ abbstraact ffactooryinterrfacee IAnnimallFacttory ICatt creeateCCat();IDogg creeateDDog();/ abbstraact pproduuctinterrfacee ICaat voidd eatt();interrfacee IDoog voidd eatt();/ cooncreete pproduuctclasss
10、BlaackCaat immplemmentss ICaat Oveerriddepubllic voidd eatt() Sysstem.out.prrintlln(tthe bblackk catt is eatiing);classs WhiiteCaat immplemmentss ICaat Oveerriddepubllic voidd eatt() Sysstem.out.pprinttln(the whitte caat iss eatting);classs BlaackDoog implemmentss IDoog Oveerriddepubllic voidd eatt(
11、) Sysstem.out.pprinttln(the blacck doog iss eatting);classs WhiiteDoog immplemmentss IDoog Oveerriddepubllic voidd eatt() Sysstem.out.pprinttln(the whitte doog iss eatting);/ cooncreeate facttoryclasss BlaackAnimaalFacctoryy impplemeents IAniimalFFactoory Oveerriddepubllic IICat creaateCaat() rettur
12、n new BBlackkCat();Oveerriddepubllic IIDog creaateDoog() retturn new BBlackkDog();classs WhiiteAnnimallFacttory impllemennts IIAnimmalFaactorry Oveerriddepubllic IICat createeCat() retturn new WWhiteeCat();Oveerriddepubllic IIDog creaateDoog() retturn new WWhiteeDog();输出结果:the bblackk catt is eatiin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 设计 模式 创造 介绍 2151

限制150内