java_Java的23种设计模式_疯狂Java总结_

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JavaJavaJavaJava1.……21.1……31.1.1……31.1.2……51.1.3……91.1.4……121.1.5……141.2……161.2.1……161.2.2……181.2.3……211.2.4……251.2.5……271.2.6……311.2.7……331.3……361.3.1……361.3.2……391.3.3……421.3.4……441.3.5……481.3.6……511.3.7……531.3.8……571.3.9……601.3.10……621.3.11……641JavaJava1.)(*JavaJ*va*******J*vaJavaJava*JavaJavaEEJavaJava*E*JavaJavaEE*Jav*IDJavaJavaEE*yangenxiong@163.comJava*Apache2.0()Apache2.02JavaJava1.1AbstractFactory()FactoryMethod()Singleton()Builder()Protot*pe*)1.1.1*FactoryMethod*1.2.3.***1.Product2.ConcreteProductProduct3.CreatorProduct*CreatorConcreteProductProduct4.ConcreteCreatorConcreteProduct3JavaJava*roductpublicinterfaceWork{voiddoWork();}ConcreteProductpublicclassStudentWorkimplementsWork{publicvoiddoWork(){System.out.println(*!);}}publicclassTeacherWorkimplementsWork{publicvoiddoWork(){System.out.println(!);}}CreatorpublicinterfaceIWorkFactory{Workget*ork();}Concre*eCreatorpu*licclassStudentWorkFactoryimplementsIWorkFactory{4JavaJavapublicWorkgetWork(){*eturnnewStudentWork();}}publicclassTeacherWorkFactoryimplementsIWorkFactory{publicWorkgetWork(){returnnewTeacherWork();}}TestpublicclassTest{publicstaticvoidm*in(Strin*[]args){IWorkFactorystudentWorkFactory=newStudentWorkFactory();studentWorkFactory.getWork().d*Work();IWorkFactoryteacherWorkFactory*newTeacherWorkFactory();teacherWorkFactory.g*tWork().*oWork();}}result!!1.1.21.*2.3.*4**5JavaJava1.Ab*tractFactory2.ConcreteFactory*.AbstractProduct4.ConcreteProdu*t**bstractProduct5.ClientAbstractFactoryAbstractProduc**bstractFactorypublicinterfaceIAn*malFactory{ICatcreateCat();IDogcre*teDog();}ConcreteFactory6JavaJavap*blicclassBlackAnimalFactoryimplem*ntsIAnimalFactory{publicICatcreateCat(){retur*newBlackCat();}publicIDogcreateDog(){returnnewBlackDog();}}publicclassWhiteAnimalFac*oryimp*ementsIAnimalFactory{publicICatcreateCat(){returnnewWhiteCat();}publicIDogcre*teDog(){returnnewWhiteDog();}}Abstrac*ProductpublicinterfaceICat{voideat();}publicinterfaceIDog{voideat();}Concrete*roductpublicclassBlack*atimplementsICat{publicvoideat(){System.out.println(Thebl*ckcatiseating!);}}7JavaJavapublicclassWhiteCatimplements*Cat{publicvoideat(){Sy*tem.out.prin*ln(Thew*itecatiseating!*);}}publicclassBlackDogimplementsIDog{publicvoideat(){System.out.println(Theblackdogiseating);}}publicclassWhiteDogimplementsIDog{publicvoideat(){System.out.println(Thewhitedogiseat*ng!);}}Clientpublicstaticvoidmain(String[]args){IAnimalFactoryblackAnimalFa*tory=newBlackAnimalFactory();ICatblackCat=blackAnimalFactory.createCat();blackCat.eat();IDogblackD*g=blackAnimalFactory.createDog();blackDog.eat();IAnimalFactorywhiteAnimalF*ctory=newWhiteAnimalFactory();ICatwhiteCat=whiteAnimalFactory.createCat();whiteCat.eat();IDog*hiteDog=whiteAnimalFactory.createDog();whiteDog.eat();}res*ltThebla*kcatiseating!Th*blackdogiseatin*!Thewhitecatiseating!8JavaJavaThewhitedogis*ating!1.1.3**1.*.**1.BuilderProduct2.ConcreteBuilderBuil*er*3.DirectorBuilder4.ProductConcreteBuilder9JavaJavaBuil*erpublicinterfacePersonBuilder{voidbuildHead();v*idbuildBody();voidbuildFoot()*PersonbuildPerson();}ConcreteBuilderpublicclassManBuilderimplementsPersonB*ilder{Personperson;publicManBuilder(){person=ne*Man();}publ*cvoidbuild*ody(){perso*.setBody();10JavaJava}publicvoidbuildFoot(){person.setFo*t();}publicvoidbuildHead(){pers*n.setHead(*);}*ublicPersonbuildPerson(){retur*person;}}Dir*ctorpublicclassPersonDirec*or{publicPersonconstructPerson(PersonBuilderpb){pb.buildHead();pb.buildBody();pb.buildFoot();returnpb.buildPerson();}}ProductpublicclassPerson{privateStringhead;privateStringbody;privateStringfoot;publicStringgetH*ad(){returnhead;}publicvoidsetHead(Stringhea*){this.head=head;}publicStringgetBody(){11JavaJavareturnbody;}publicvoidsetBody(Stringbody){this.b*dy=body;}publicStringgetFoot(){returnfoot;}publicvoidsetFoot(Stringfoot){t*is.foot=foot;}}publicclassManextendsPerson{}Testpubl*cclassTest{publicstaticvoidmain(String[]ar*s){PersonDirectorpd=newPersonDirector();Personperson=pd.constructPerson(newManBuilder());System*out.println(person.getBody());System.out.println(person.getFoot());System.out.println(person.getHead());}}result**1.1.4**1.*12JavaJava2.SingletonInstanceInstance*SingletonpublicclassSingleton{privatestaticSingletonsing;privateSingleton(){}publicst*ticSingletonget*nstance(){if(sing==null){sing=newSingleto*();}returnsing;}}TestpublicclassTest{publicstaticvoid*ain(*tring[]args){Singletonsing=Singleton.getInstance();Singletonsi*g2=Singleton.getI*stance();System.out.println(sing);System.out.pr*ntln(sing2);}}result13JavaJavasingleton.Singleton@1c78e57singleton.Singleton@1c78e571.1.51.*2.3.*4.1.Prototype2.ConcretePrototype3.Client*C:\Users\fulin\AppData\Local\Temp\404683\images\Prototype.jpgPrototype14JavaJavapublicclassPrototypeimplementsCloneable{privateStringname;publicvoidsetName(Stringname){this.name=name;}publicStringgetName(){returnthis.name;}publicObjectclone(){try{returnsuper.clone();}catch(Exceptione){e.printStackTrace();returnnull;}}}ConcretePrototypepubl*cclassConcret

1 / 31
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功