欢迎来到天天文库
浏览记录
ID:22013676
大小:1.84 MB
页数:35页
时间:2018-10-21
《创建型设计模式 培训(英文)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、BryanLi6/18/2007Common&DifferenceofDesignPattern™CreationalPatternandRefactorAgendaOODesignPrincipleOCP(Open-ClosedPrinciple)LSP(LiskovSubstitutionPrinciple)SRP(singleresponsibilityPrinciple)DIP(DependenceInversionPrinciple)ISP(InterfaceSegregationPrinciple)CARP(Composite/AggregateReusePrinciple)LoD
2、(LasodDemeter—LeastKnowledgePrinciple-LKP)DesignPatternCreationalPatternStructuralPatternBehavioralPatternAgendaCreationalPatternFactoryPatternSimpleFactoryFactoryMethodAbstractFactoryFactoryPatternSimpleFactorySimplymovedthecodethatisusedtocreateobjecttoanewclassCreator.FactoryPatternPizzaStoryYouw
3、anttoopenapizzastore,Assumethatyouorganizelikethis:SimplePatternpublicclassPizzaStore{PizzaorderPizza(Stringtype){Pizzapizza=null;if(type.equals("chees")){pizza=newCheesePizza();}elseif(type.equals("greek")){pizza=newGreekPizza();}elseif(type.equals("pepperoni")){pizza=newPepperoniPizza();}pizza.pre
4、pare();pizza.bake();pizza.cut();pizza.box();returnpizza;}}SimplePatternMendingdesignofpizzastorySimplePatternAddSimplePizzaFactoryClasspublicclassSimplePizzaFactory{publicstaticPizzacreatePizza(Stringtype){Pizzapizza=null;if(type.equals("chees")){pizza=newCheesePizza();}elseif(type.equals("greek")){
5、pizza=newGreekPizza();}elseif(type.equals("pepperoni")){pizza=newPepperoniPizza();}returnpizza;}}publicclassPizzaStore{SimplePizzaFactoryfactory;publicPizzaorderPizza(Stringtype){Pizzapizza=SimplePizzaFactory.createPizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();returnpizza;}}Simple
6、PatternFeatureyoudon'tneedtoinstantiateanobjecttomakeuseofthecreatemethodyoucan'tcreatesublcassandchangethebehaviorofthecreatemethodNottypicaldesignpattern--moreofaprogrammingidiomFadeFormatMergeCreatorintoConcreteProductSimplePatternAdvantageReusethecodethatcreateobjectDisadvantageIfproducthasanych
7、ange,Creatorclassshouldbeimpacted.OODesignPrincipleOCP:ifaddnewtypeofproduct,intheviewofclientandproduct,itconformtoOCP,butintheviewofFactoryCreator,itdoesnot.FactoryMethodDefineaninterfaceforcreating
此文档下载收益归作者所有