1、单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本
2、样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,单击此处编辑母版标题样式,单击此处编辑母版文本样式,二级,三级,四级,五级,*,Bryan Li,6/
3、18/2007,Common&Difference of Design Pattern,Creational Pattern and Refactor,Agenda,OO Design Principle,OCP(Open-Closed Principle),LSP(Liskov Substitution Principle),SRP(single responsibility Principle),DIP(Dependence Inversion Principle),ISP(Interface Segregation Principle),CARP(Composite/Aggregate
4、Reuse Principle),LoD(Las od DemeterLeast Knowledge Principle-LKP),Design Pattern,Creational Pattern,Structural Pattern,Behavioral Pattern,Agenda,Creational Pattern,Factory Pattern,Simple Factory,Factory Method,Abstract Factory,Factory Pattern,Simple Factory,Simply moved the code that is used to crea
5、te object to a new class Creator.,Factory Pattern,Pizza Story,You want to open a pizza store,Assume that you organize like this:,Simple Pattern,public class,PizzaStore,Pizza,orderPizza,(String type),Pizza pizza=null;,if(type.equals(chees),pizza=new CheesePizza();,else if(type.equals(greek),pizza=new
6、 GreekPizza();,else if(type.equals(pepperoni),pizza=new PepperoniPizza();,pizza.prepare();,pizza.bake();,pizza.cut();,pizza.box();,return pizza;,Simple Pattern,Mending design of pizza story,Simple Pattern,Add SimplePizzaFactory Class,public class,SimplePizzaFactory,public,static,Pizza,createPizza,(S
7、tring type),Pizza pizza=null;,if(type.equals(chees),pizza=new CheesePizza();,else if(type.equals(greek),pizza=new GreekPizza();,else if(type.equals(pepperoni),pizza=new PepperoniPizza();,return pizza;,public class,PizzaStore,SimplePizzaFactory factory;,public Pizza,orderPizza,(String type),Pizza piz
8、za=,SimplePizzaFactory.createPizza(type),;,pizza.prepare();,pizza.bake();,pizza.cut();,pizza.box();,return pizza;,Simple Pattern,Feature,you dont need to instantiate an object to make use of the create method,you cant create sublcass and change the behavior of the create method,Not typical design pa
9、ttern-more of a programming idiom,Fade Format,Merge Creator into ConcreteProduct,Simple Pattern,Advantage,Reuse the code that create object,Disadvantage,If product has any change,Creator class should be impacted.,OO Design Principle,OCP:if add new type of product,in the view of client and product,it
10、 conform to OCP,but in the view of Factory Creator,it does not.,Factory Method,Define an interface for creating an object,but lets subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses.,Factory Method,Pizza Story,Because your pizza store has a goo
11、d business,many people want to open your sub store in New York and Chicago.So you will the localize favor problem.,Based on simple factory,design like the following:,Factory Method,The above designs Issue,Because simpele factory generally use static create method.So if there are several simple facto
12、ry creators,it will impact encapsulation of client class,that is PizzaStore.,public class,PizzaStore,SimplePizzaFactory factory;,public Pizza,orderPizza,(String type),Pizza pizza=,SimplePizzaFactory.createPizza(type),;,/or NYPizzaFactory.createPizza(type),;,/or ChicagoPizzaFactory.createPizza(type),
13、pizza.prepare();,pizza.bake();,pizza.cut();,pizza.box();,return pizza;,Factory Method,The above designs Issue,if simpele factory generally use non-static create method and other factories derive from simplePizzaFactory.So What?,Factory Method,public class,PizzaStore,SimplePizzaFactory,factory,;,pu
14、blic PizzaStore(SimplePizzaFactory factory),this.factory=factory;,public Pizza,orderPizza,(String type),Pizza pizza=,factory.createPizza(type),;,pizza.prepare();,pizza.bake();,pizza.cut();,pizza.box();,return pizza;,public class Client,public static void main(String args),NYPizzaFactory nyfactory=ne
15、w NYPizzaFactory();,PizzaStore nyStore=new PizzaStore(nyfactory);,nyStore.orderPizza(Veggie);,Factory Method,New requirement bring new issue,Now New York pizza stores procedure of make pizza was changed:theyd bake things a little differently,they neednt cut the pizza and theyd use third-party boxes.
16、So we must have different pizza store class in order to solve this issue.,Add subclass,Factory Method,Refactor the design,Now the hierarchy of Pizzastore completely shows the hierarchy of simple factory.So now the series of factory class are redundancy.We should move the method of factory into the s
17、eries classes of Pizzastore.The refactored design is as the below:,Client,Factory Method,Product,Factory Method,public abstract class,PizzaStore,public Pizza,orderPizza,(String type),Pizza pizza=createPizza(type);,doHandlePizza(pizza);,return pizza;,protected void,doHandlePizza,(Pizza pizza),pizza.p
18、repare();,pizza.bake();,pizza.cut();,pizza.box();,public abstract Pizza,createPizza,(String type);,Factory Method,public class,NYStylePizzaStore,extends,PizzaStore,public Pizza,createPizza,(String type),Pizza pizza=null;,if(type.equals(chees),pizza=new NYStyleCheesePizza();,else if(type.equals(greek
19、),pizza=new NYStyleGreekPizza();,else if(type.equals(pepperoni),pizza=new NYStylePepperoniPizza();,return pizza;,protected void,doHandlePizza,(Pizza pizza),pizza.prepare();,pizza.bake();,/pizza.cut();,pizza.box();,Factory Method,public class,ChicagoStylePizzaStore,extends,PizzaStore,public Pizza,cre
20、atePizza,(String type),Pizza pizza=null;,if(type.equals(chees),pizza=new ChicagoStyleCheesePizza();,else if(type.equals(greek),pizza=new ChicagoStyleGreekPizza();,else if(type.equals(pepperoni),pizza=new ChicagoStylePepperoniPizza();,return pizza;,Factory Method,Feature,The Factory Method lets subcl
21、asses decide which class to instantiate.,A factory method may be parameterized(or not)to select among several variations of a product.,FM doesnt certainly return new object very time.But returned object must be created by FM itself,but not be passed from outside.,Returned type should be abstract typ
22、e,not concrete type.,Factory Method,Advantage,the creator/client class is written without knowledge of the actual products that will be created;that is clients decoupled from the concret types.,Disadvantage,Clients might have to subclass the Creator class just to create a particular ConcreteProduct
23、object.,With OO Design Principle,OCP:in the view of client/creator,it conform to OCP,but in the view of product,not certainly.In general,in FMP,there is a only kind of product.If there are several kinds,need parameterized create method like createPizza method.So if add product kind,all client/creato
24、r will be impacted.So not obey OCP.,Abstract Factory,Provide an interface for creating families of related or dependent objects without specifying their concrete classes.,Abstract Factory,Pizza story,Pizza from different place has different ingredients,PlumTomatoSauce,ThickCrustDough,MozzarellaChees
25、e,FrozenPepperoni,New York,MarinaraSauce,ThinCrustDough,ReggianoCheese,FreshPepperoni,Chicago,Abstract Factory,Close coupling,Over-deep hierarchy,Abstract Factory,Pizza story part of class diagram,Abstract Factory,public interface,Pizza,void prepare();,void bake();,void cut();,void box();,public abs
26、tract class,PepperoniPizza,implements,Pizza,protected Dough dough;,protected Sauce sauce;,protected Cheese cheese;,protected Pepperoni pepperoni;,public abstract void,prepare,();,public void,box,()System.out.println(box pepperoni pizza.);,public void,bake,()System.out.println(bake pepperoni pizza.);
27、public void,cut,()System.out.println(cut pepperoni pizza.);,Abstract Factory,public class,NYStylePepperoniPizza,extends,PepperoniPizza,public void,prepare,(),dough=new ThickCrustDough();,sauce=new PlumTomatoSauce();,cheese=new MozzarellaCheese();,pepperoni=new FrozenPepperoni();,public class,NYStyl
28、eCheesePizza,extends,CheesePizza,public void,prepare,(),dough=new ThickCrustDough();,sauce=new PlumTomatoSauce();,cheese=new MozzarellaCheese();,public class,NYPizzaIngredientFactory,public Dough,createDough,(),return,new ThickCrustDough(),;,public Sauce,createSauce,(),return,new PlumTomatoSauce(),;
29、public Cheese,createCheese,(),return,new MozzarellaCheese(),;,public Pepperoni,createPepperoni,(),return,new FrozenPepperoni(),;,Abstract Factory,Use refactor“extract interface”to get PizzaIngredientFactory interface in NYPizzaIngredientFactory class.,public interface,PizzaIngredientFactory,public
30、abstract Dough,createDough,();,public abstract Sauce,createSauce,();,public abstract Cheese,createCheese,();,public abstract Pepperoni,createPepperoni,();,public class,NYPizzaIngredientFactory,implements,PizzaIngredientFactory,.,public class,NYPizzaIngredientFactory,public Dough,createDough,(),retur
31、n,new ThickCrustDough(),;,public Sauce,createSauce,(),return,new PlumTomatoSauce(),;,public Cheese,createCheese,(),return,new MozzarellaCheese(),;,public Pepperoni,createPepperoni,(),return,new FrozenPepperoni(),;,Abstract Factory,public class,ChicagoStyleCheesePizza,extends,CheesePizza,public void,
32、prepare,(),dough=new ThinCrustDough();,sauce=new MarinaraSauce();,cheese=new ReggianoCheese();,public class,NYStyleCheesePizza,extends,CheesePizza,public void,prepare,(),dough=new ThickCrustDough();,sauce=new PlumTomatoSauce();,cheese=new MozzarellaCheese();,public class,ChicagoStyleCheesePizza,exte
33、nds,CheesePizza,PizzaIngredientFactory,factory,;,public,ChicagoStyleCheesePizza,(),factory,=new,ChicagoPizzaIngredientFactory,();,public void,prepare,(),dough=factory.createDough();,sauce=factory.createSauce();,cheese=factory.createCheese();,public class,NYStyleCheesePizza,extends,CheesePizza,PizzaI
34、ngredientFactory,factory,;,public NYStyleCheesePizza(),factory,=new,NYPizzaIngredientFactory,();,public void,prepare,(),dough=factory.createDough();,sauce=factory.createSauce();,cheese=factory.createCheese();,Abstract Factory,Use refactor“pull up”to restract factory property and prepare method into
35、cheesePizza.And then we can delete,ChicagoStyleCheesePizza,and,NYStyleCheesePizza,because combination of,CheesePizza,and series of,IngredientFactory,will get these two favors pizza.In the same way,we can delete,ChicagoStylePepperoniPizza,and,NYStylePepperoniPizza,.,public abstract class,CheesePizza,
36、implements,Pizza,protected Dough,dough,;,protected Sauce,sauce,;,protected Cheese,cheese,;,protected PizzaIngredientFactory,factory,;,public,CheesePizza,(PizzaIngredientFactory factory)this.factory=factory;,public void,prepare,(),dough=factory.createDough();,sauce=factory.createSauce();,cheese=facto
37、ry.createCheese();,public void,box,(),public void,bake,(),public void,cut,(),Abstract Factory,Abstract Factory,Product,Client,Client,Abstract Factory,Feature,Provides an interface for a family of products.,Create Instance through Composition/Aggregation.,Doesnt certainly return new object very time.
38、But returned object must be created by AF itself,but not be passed from outside.,Returned type should be abstract type,not concrete type.,Abstract Factory,Advantage,Clients are decoupled from the actual concrete products they use.,Adding new family of product is easy,Disadvantage,Adding new kind of
39、product is difficult.,With OO Design Principle,OCP:in the view of Adding new family of product,it conform to OCP,but in the view of Adding new kind of product,it doesnt.,CARP:that client create product through factory conform to CARP,FMP vs.AFP,Common,Create Product.Furthmore,client know nothing abo
40、ut knowledge of concrete product.,Difference,FMP is for one kind of product,AFP is for several kinds of product.Q:Right?A:,Wrong!,so its not difference,FMP create product through inheritance;AFP create product through composition/aggregation.,The relationship between MFP and AFP is similar to the one between Template Method Pattern and Strategy Pattern.,此课件下载可自行编辑修改,供参考!,感谢您的支持,我们努力做得更好!,






