资源描述:
《设计模式袖珍版连续转载之-factory》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、设计模式袖珍版连续转载之-FactoryTheeasiervanmistakes・Thateveryonehasdonedetailedworkhaveexperieneegreatly,onthecontrary,thehighertheofficerisdone,sayingthemoreabstractandgeneral,fanerrorpossibilityistheless・Asifwecould,velearnedinlifefromtheprogram?Haha.Usethefa
2、ctorymethodAttentionshouldbepaidtoseveralcharacters,firstyouhavetodefineproductinterface,asSample,theproductwiththeSampleundertheinterfaceinterfaceimplementationclass,suchasSampleA,secondtohaveafactoryclass,usedtoproducetheproductSample,thefollowingf
3、igure,mosttherightsideistheproductionofSampleobjects:Afurthercomplicationistheexpansionofthefactoryclass,whichalsoinheritsitsimplementationclass,concreteFactory.TheabstractfactoryThereisaFactoryMethod:theFactoryMethodAbstractFactory.Thediffereneebetw
4、eenthesetwopatternsisthecomplexityofcreatingobjects・Ifthemethodforcreatinganobjectbecomesmorecomplex,asinthefactorymethodabove,wecreateanobjectSample,ifwehaveanewproductinterfaceSample2・Let'sassume:SamplehastwoconcreteclassesSampleAandSamleB,andSampl
5、e2alsohastwoconcreteclassesSample2AandSampleB2So,wewillintheaboveexampleFactoryintoanabstractclass,partoftheencapsulationtogetherinanabstractclass,usesubclassesimplementdifferentparts,thefollowingistheFactorytoexpandintotheabstractFactoryintheaboveex
6、ample:PublicabstractclassFactory{PublicabstractSamplecreator();PublicabstractSample2creator(Stringname);PublicclassSimpleFactoryextendsFactory{PublicSamplecreator(){ReturnnewSampleAPublicSample2creator(Stringname){ReturnnewSample2APublicclassBombFact
7、oryextendsFactory{PublicSamplecreator(){ReturnnewSampleBPublicSample2creator(Stringname){ReturnnewSample2BFromabove,youcanseethatthetwofactorieseachproduceonesetofSampleandSample2,andyoumaywonderwhyIcan,tusetwofactorymethodstoproduceSampleandSample2s
8、eparately.Abstractfactorythereisanotherkeypoint,becauseSimpleFactory,productionSampleandproductionmethodshaveacertainrelationshipbetweentheSample2,sowewanttobindthetwomethodsinaclass,thisfactoryhasitsowncharacteristics,manufacturingprocessmaybeunifie