版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Bryan Li6/18/2007Common & Difference of Design PatternCreational Pattern and Refactor整理ppt2AgendaOO Design PrincipleOCP(Open-Closed Principle)LSP(Liskov Substitution Principle)SRP(single responsibility Principle)DIP(Dependence Inversion Principle)ISP(Interface Segregation Principle)CARP(Composit
2、e/Aggregate Reuse Principle)LoD(Las od DemeterLeast Knowledge Principle-LKP)Design PatternCreational PatternStructural PatternBehavioral Pattern整理ppt3AgendaCreational PatternFactory PatternSimple FactoryFactory MethodAbstract Factory整理ppt4Factory PatternSimple FactorySimply moved the code that is us
3、ed to create object to a new class Creator.整理ppt5Factory PatternPizza StoryYou want to open a pizza store, Assume that you organize like this:整理ppt6Simple Patternpublic class PizzaStore Pizza orderPizza(String type) Pizza pizza = null;if(type.equals(chees) pizza = new CheesePizza(); else if (type.eq
4、uals(greek) pizza = new GreekPizza(); else if (type.equals(pepperoni) pizza = new PepperoniPizza();pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;整理ppt7Simple PatternMending design of pizza story整理ppt8Simple PatternAdd SimplePizzaFactory Classpublic class SimplePizzaFactory public
5、 static Pizza createPizza(String 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
6、orderPizza(String type) Pizza pizza = SimplePizzaFactory.createPizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;整理ppt9Simple PatternFeatureyou dont need to instantiate an object to make use of the create method you cant create sublcass and change the behavior of the crea
7、te methodNot typical design pattern- more of a programming idiomFade FormatMerge Creator into ConcreteProduct 整理ppt10Simple PatternAdvantageReuse the code that create objectDisadvantageIf product has any change, Creator class should be impacted.OO Design PrincipleOCP: if add new type of product, in
8、the view of client and product, it conform to OCP, but in the view of Factory Creator, it does not.整理ppt11Factory MethodDefine an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.整理ppt12Factory Meth
9、odPizza StoryBecause your pizza store has a good 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:整理ppt13Factory MethodThe above designs IssueBecause simpele factory generally use stat
10、ic create method. So if there are several simple factory 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.createPi
11、zza(type); / or ChicagoPizzaFactory.createPizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza; 整理ppt14Factory MethodThe above designs Issueif simpele factory generally use non-static create method and other factories derive from simplePizzaFactory. So What?整理ppt15Factory Me
12、thodpublic class PizzaStore SimplePizzaFactory factory;public 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 stati
13、c void main(String args) NYPizzaFactory nyfactory = new NYPizzaFactory();PizzaStore nyStore = new PizzaStore(nyfactory);nyStore.orderPizza(Veggie); 整理ppt16Factory MethodNew requirement bring new issueNow New York pizza stores procedure of make pizza was changed: theyd bake things a little differentl
14、y, they neednt cut the pizza and theyd use third-party boxes. So we must have different pizza store class in order to solve this issue.Add subclass整理ppt17Factory MethodRefactor the designNow the hierarchy of Pizzastore completely shows the hierarchy of simple factory. So now the series of factory cl
15、ass are redundancy. We should move the method of factory into the series classes of Pizzastore. The refactored design is as the below:ClientFactory MethodProduct整理ppt18Factory Methodpublic abstract class PizzaStore public Pizza orderPizza(String type) Pizza pizza = createPizza(type);doHandlePizza(pi
16、zza);return pizza;protected void doHandlePizza(Pizza pizza) pizza.prepare();pizza.bake();pizza.cut();pizza.box();public abstract Pizza createPizza(String type);整理ppt19Factory Methodpublic class NYStylePizzaStore extends PizzaStore public Pizza createPizza(String type) Pizza pizza = null;if (type.equ
17、als(chees) pizza = new NYStyleCheesePizza(); else if (type.equals(greek) 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(); 整理ppt20Factory M
18、ethodpublic class ChicagoStylePizzaStore extends PizzaStore public Pizza createPizza(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 ChicagoStyl
19、ePepperoniPizza();return pizza;整理ppt21Factory MethodFeatureThe Factory Method lets subclasses 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 c
20、reated by FM itself, but not be passed from outside.Returned type should be abstract type, not concrete type.整理ppt22Factory MethodAdvantagethe creator/client class is written without knowledge of the actual products that will be created; that is clients decoupled from the concret types.DisadvantageC
21、lients might have to subclass the Creator class just to create a particular ConcreteProduct object.With OO Design PrincipleOCP: 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
22、, need parameterized create method like createPizza method. So if add product kind, all client/creator will be impacted. So not obey OCP. 整理ppt23Abstract FactoryProvide an interface for creating families of related or dependent objects without specifying their concrete classes.整理ppt24Abstract Factor
23、yPizza storyPizza from different place has different ingredientsPlumTomatoSauceThickCrustDoughMozzarellaCheeseFrozenPepperoniNew YorkMarinaraSauceThinCrustDoughReggianoCheeseFreshPepperoniChicago整理ppt25Abstract FactoryClose couplingOver-deep hierarchy整理ppt26Abstract FactoryPizza story part of class
24、diagram整理ppt27Abstract Factorypublic interface Pizza void prepare();void bake();void cut();void box();public abstract class PepperoniPizza implements Pizza protected Dough dough;protected Sauce sauce;protected Cheese cheese;protected Pepperoni pepperoni;public abstract void prepare();public void box
25、() System.out.println(box pepperoni pizza.); public void bake() System.out.println(bake pepperoni pizza.); public void cut() System.out.println(cut pepperoni pizza.); 整理ppt28Abstract Factorypublic class NYStylePepperoniPizza extends PepperoniPizza public void prepare() dough = new ThickCrustDough();
26、sauce = new PlumTomatoSauce();cheese = new MozzarellaCheese();pepperoni = new FrozenPepperoni();public class NYStyleCheesePizza extends CheesePizza public void prepare() dough = new ThickCrustDough();sauce = new PlumTomatoSauce();cheese = new MozzarellaCheese();public class NYPizzaIngredientFactory
27、public Dough createDough() return new ThickCrustDough(); public Sauce createSauce() return new PlumTomatoSauce(); public Cheese createCheese() return new MozzarellaCheese(); public Pepperoni createPepperoni() return new FrozenPepperoni(); 整理ppt29Abstract FactoryUse refactor “extract interface” to ge
28、t PizzaIngredientFactory interface in NYPizzaIngredientFactory class.public interface PizzaIngredientFactory public abstract Dough createDough();public abstract Sauce createSauce();public abstract Cheese createCheese();public abstract Pepperoni createPepperoni();public class NYPizzaIngredientFactory
29、 implements PizzaIngredientFactory.public class NYPizzaIngredientFactory public Dough createDough() return new ThickCrustDough(); public Sauce createSauce() return new PlumTomatoSauce(); public Cheese createCheese() return new MozzarellaCheese(); public Pepperoni createPepperoni() return new FrozenP
30、epperoni(); 整理ppt30Abstract Factorypublic class ChicagoStyleCheesePizza extends CheesePizza public void prepare() dough = new ThinCrustDough();sauce = new MarinaraSauce();cheese = new ReggianoCheese();public class NYStyleCheesePizza extends CheesePizza public void prepare() dough = new ThickCrustDou
31、gh();sauce = new PlumTomatoSauce();cheese = new MozzarellaCheese();public class ChicagoStyleCheesePizza extends CheesePizza PizzaIngredientFactory factory;public ChicagoStyleCheesePizza() factory = new ChicagoPizzaIngredientFactory(); public void prepare() dough = factory.createDough();sauce = facto
32、ry.createSauce();cheese = factory.createCheese(); public class NYStyleCheesePizza extends CheesePizza PizzaIngredientFactory factory;public NYStyleCheesePizza() factory = new NYPizzaIngredientFactory(); public void prepare() dough = factory.createDough();sauce = factory.createSauce();cheese = factor
33、y.createCheese(); 整理ppt31Abstract FactoryUse refactor “pull up” to restract factory property and prepare method into cheesePizza. And then we can delete ChicagoStyleCheesePizza and NYStyleCheesePizza because combination of CheesePizza and series of IngredientFactory will get these two favors pizza.
34、In the same way, we can delete ChicagoStylePepperoniPizza and NYStylePepperoniPizza.public abstract class CheesePizza 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 = factory.createCheese(); public void box() public void bake() public void cut() 整理ppt32Abstract FactoryAbstract FactoryProductClientClient整理ppt33Abstract F
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医博士科护理培训考试题库及答案
- 交通安全法规学习:2026年驾驶员考试及答案
- 2026三年级数学上册 四边形单元测试
- 2026六年级数学上册 分数除法建模能力
- 2026二年级数学下册 克和千克关键能力
- 体检者隐私保护制度
- 2026八年级下语文修辞手法评价技巧
- 人力资源四项制度
- 五官科住院部制度
- 车间绩效及质量奖惩制度
- 2026年安徽城市管理职业学院单招职业适应性测试题库带答案详解(新)
- 应急管理干部警示教育以案促改心得体会
- 冀教版八年级英语下册Lesson28 Ms Lius Great Idea 核心素养教案
- 2026年小学六年级下册劳动教育教学计划
- 2026春小学科学青岛版(五四制2024)三年级下册教学计划、教案及每课教学反思(附目录)
- 2026年内蒙古化工职业学院单招综合素质考试题库及一套参考答案详解
- 2026上海交通大学医学院招聘91人考试备考题库及答案解析
- 2026年南京铁道职业技术学院单招职业适应性考试题库附答案详解(夺分金卷)
- 2026年春季人教PEP版五年级下册英语教学计划含教学进度表
- (2026年)海姆立克法急救培训课件
- 湖北2025年湖北科技学院招聘19人笔试历年参考题库附带答案详解
评论
0/150
提交评论