版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Bryan Li6/18/2007,?,Agenda,OO Design PrincipleOCP(Open-Closed Principle)LSP(Liskov Substitution Principle)SRP(single responsibility Principle)DIP(Dependence Inversion Principle)ISP(Interface Segregation Principle)
2、CARP(Composite/Aggregate Reuse Principle)LoD(Las od Demeter—Least Knowledge Principle-LKP)Design PatternCreational PatternStructural PatternBehavioral Pattern,Agenda,Creational PatternFactory PatternSimple Factory
3、Factory MethodAbstract Factory,Factory Pattern,Simple FactorySimply moved the code that is used to create object to a new class Creator.,Factory Pattern,Pizza StoryYou want to open a pizza store, Assume that you orga
4、nize 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")) {pi
5、zza = new 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 s
6、tory,Simple Pattern,Add SimplePizzaFactory Classpublic class SimplePizzaFactory {public static Pizza createPizza(String type) {Pizza pizza = null;if (type.equals("chees")) {pizza = new CheesePizza();} els
7、e if (type.equals("greek")) {pizza = new GreekPizza();} else if (type.equals("pepperoni")) {pizza = new PepperoniPizza();}return pizza;}},public class PizzaStore {SimplePizzaFactory factory;p
8、ublic Pizza orderPizza(String type) {Pizza pizza = SimplePizzaFactory.createPizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;}},Simple Pattern,Featureyou don't need to instantiat
9、e an object to make use of the create method you can't create sublcass and change the behavior of the create methodNot typical design pattern-- more of a programming idiomFade FormatMerge Creator into ConcretePro
10、duct,Simple Pattern,AdvantageReuse the code that create objectDisadvantageIf product has any change, Creator class should be impacted.OO Design PrincipleOCP: if add new type of product, in the view of client and pro
11、duct, it 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 defe
12、r instantiation to subclasses.,Factory Method,Pizza 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 si
13、mple factory, design like the following:,Factory Method,The above design's IssueBecause simpele factory generally use static create method. So if there are several simple factory creators, it will impact encapsulati
14、on of client class, that is PizzaStore.public class PizzaStore {SimplePizzaFactory factory;public Pizza orderPizza(String type) {Pizza pizza = SimplePizzaFactory.createPizza(type); // or NYPizzaFactory.createPizza(
15、type); // or ChicagoPizzaFactory.createPizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza; } },Factory Method,The above design's Issueif simpele factory generally use non-static cre
16、ate method and other factories derive from simplePizzaFactory. So What?,Factory Method,public class PizzaStore {SimplePizzaFactory factory;public PizzaStore(SimplePizzaFactory factory) {this.factory = factory;}publi
17、c 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) {NYPizza
18、Factory nyfactory = new NYPizzaFactory();PizzaStore nyStore = new PizzaStore(nyfactory);nyStore.orderPizza("Veggie");} },Factory Method,New requirement bring new issueNow New York pizza store's procedur
19、e of make pizza was changed: they'd bake things a little differently, they needn't cut the pizza and they'd use third-party boxes. So we must have different pizza store class in order to solve this issue.,Add
20、 subclass,Factory Method,Refactor the designNow 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
21、the series 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(ty
22、pe);doHandlePizza(pizza);return pizza;}protected void doHandlePizza(Pizza pizza) {pizza.prepare();pizza.bake();pizza.cut();pizza.box();}public abstract Pizza createPizza(String type);},Factory Method,public cl
23、ass NYStylePizzaStore extends PizzaStore {public Pizza createPizza(String type) {Pizza pizza = null;if (type.equals("chees")) {pizza = new NYStyleCheesePizza();} else if (type.equals("greek")) {
24、pizza = new NYStyleGreekPizza();} else if (type.equals("pepperoni")) {pizza = new NYStylePepperoniPizza();}return pizza;}protected void doHandlePizza(Pizza pizza) {pizza.prepare();pizza.bake();//pizza.
25、cut();pizza.box();} },Factory Method,public class ChicagoStylePizzaStore extends PizzaStore {public Pizza createPizza(String type) {Pizza pizza = null;if (type.equals("chees")) {pizza = new ChicagoStyleC
26、heesePizza();} else if (type.equals("greek")) {pizza = new ChicagoStyleGreekPizza();} else if (type.equals("pepperoni")) {pizza = new ChicagoStylePepperoniPizza();}return pizza;}},Factory Meth
27、od,FeatureThe 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 doesn't certainly return new object v
28、ery time. But returned object must be created by FM itself, but not be passed from outside.Returned type should be abstract type, not concrete type.,Factory Method,Advantagethe creator/client class is written without k
29、nowledge of the actual products that will be created; that is clients decoupled from the concret types.DisadvantageClients might have to subclass the Creator class just to create a particular ConcreteProduct object.Wi
30、th 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, need parameterized
31、 create method like createPizza method. So if add product kind, all client/creator will be impacted. So not obey OCP.,Abstract Factory,Provide an interface for creating families of related or dependent objects without sp
32、ecifying their concrete classes.,Abstract Factory,Pizza storyPizza from different place has different ingredients,,PlumTomatoSauce,ThickCrustDough,MozzarellaCheese,FrozenPepperoni,New York,,MarinaraSauce,ThinCrustDough,
33、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
34、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 vo
35、id box() { System.out.println("box pepperoni pizza."); }public void bake() { System.out.println("bake pepperoni pizza."); }public void cut() {System.out.println("cut pepperoni pizza."); }
36、},Abstract Factory,public class NYStylePepperoniPizza extends PepperoniPizza {public void prepare() {dough = new ThickCrustDough();sauce = new PlumTomatoSauce();cheese = new MozzarellaCheese();pepperoni = new Frozen
37、Pepperoni();}}public class NYStyleCheesePizza extends CheesePizza {public void prepare() {dough = new ThickCrustDough();sauce = new PlumTomatoSauce();cheese = new MozzarellaCheese();}},public class NYPizzaIngred
38、ientFactory {public Dough createDough() {return new ThickCrustDough(); }public Sauce createSauce() {return new PlumTomatoSauce(); }public Cheese createCheese() {return new MozzarellaCheese(); }public Pepperoni cre
39、atePepperoni() {return new FrozenPepperoni(); }},,,Abstract Factory,Use refactor “extract interface” to get PizzaIngredientFactory interface in NYPizzaIngredientFactory class.public interface PizzaIngredientFactory {
40、public abstract Dough createDough();public abstract Sauce createSauce();public abstract Cheese createCheese();public abstract Pepperoni createPepperoni();}public class NYPizzaIngredientFactory implements PizzaIngr
41、edientFactory{...},public class NYPizzaIngredientFactory {public Dough createDough() {return new ThickCrustDough(); }public Sauce createSauce() {return new PlumTomatoSauce(); }public Cheese createCheese() {return
42、 new MozzarellaCheese(); }public Pepperoni createPepperoni() {return new FrozenPepperoni(); }},Abstract Factory,public class ChicagoStyleCheesePizza extends CheesePizza {public void prepare() {dough = new ThinCrustD
43、ough();sauce = new MarinaraSauce();cheese = new ReggianoCheese();}}public class NYStyleCheesePizza extends CheesePizza {public void prepare() {dough = new ThickCrustDough();sauce = new PlumTomatoSauce();cheese
44、= new MozzarellaCheese();}},public class ChicagoStyleCheesePizza extends CheesePizza {PizzaIngredientFactory factory;public ChicagoStyleCheesePizza() {factory = new ChicagoPizzaIngredientFactory(); }public void pre
45、pare() {dough = factory.createDough();sauce = factory.createSauce();cheese = factory.createCheese(); } }public class NYStyleCheesePizza extends CheesePizza {PizzaIngredientFactory factory;public NYStyleCheesePizza
46、() {factory = new NYPizzaIngredientFactory(); }public void prepare() {dough = factory.createDough();sauce = factory.createSauce();cheese = factory.createCheese();} },,,Abstract Factory,Use refactor “pull up” to rest
47、ract 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 favor
48、s pizza. 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;pr
49、otected PizzaIngredientFactory factory;public CheesePizza(PizzaIngredientFactory factory) { this.factory = factory; }public void prepare() {dough = factory.createDough();sauce = factory.createSauce();cheese = factor
50、y.createCheese(); } }public void box() { } public void bake() { } public void cut() { }},Abstract Factory,,Abstract Factory,Product,Client,Client,Abstract Factory,FeatureProvides an interface for a family of product
51、s.Create Instance through Composition/ Aggregation.Doesn't certainly return new object very time. But returned object must be created by AF itself, but not be passed from outside.Returned type should be abstract t
52、ype, not concrete type.,AdvantageClients are decoupled from the actual concrete products they use.Adding new family of product is easyDisadvantageAdding new kind of product is difficult.With OO Design PrincipleOCP:
53、 in the view of Adding new family of product, it conform to OCP, but in the view of Adding new kind of product, it doesn't.CARP: that client create product through factory conform to CARP,FMP vs. AFP,CommonCreate P
54、roduct. Furthmore, client know nothing about knowledge of concrete product.DifferenceFMP is for one kind of product, AFP is for several kinds of product. Q: Right? A: Wrong!!! so it's not differenceFMP create prod
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 6.創(chuàng)建型設(shè)計模式吳粉俠
- 創(chuàng)建循環(huán)型工業(yè)管理模式
- “創(chuàng)建學(xué)習(xí)型醫(yī)院”學(xué)習(xí)培訓(xùn)感想
- 鄭州創(chuàng)建學(xué)習(xí)型城市的模式研究.pdf
- 關(guān)于煤礦新型安全培訓(xùn)教育模式創(chuàng)建的探討.pdf
- 創(chuàng)建型模式
- 創(chuàng)建決策型財務(wù)管理工作模式.pdf
- 學(xué)習(xí)型組織創(chuàng)建模式的評估研究.pdf
- 合作探究型教師培訓(xùn)模式研究.pdf
- 創(chuàng)建名師工作室 探索培訓(xùn)企業(yè)人才培養(yǎng)新模式
- 創(chuàng)建安全培訓(xùn)新模式 提高企業(yè)員工安全教育效果
- 中國生產(chǎn)型企業(yè)培訓(xùn)模式研究.pdf
- 平安醫(yī)院創(chuàng)建培訓(xùn)教案
- 創(chuàng)建者(builder)模式
- 在創(chuàng)建節(jié)水型城市推進暨業(yè)務(wù)培訓(xùn)會議上講話
- 知識型企業(yè)e-learning培訓(xùn)模式研究
- 國際創(chuàng)業(yè)型企業(yè)創(chuàng)建戰(zhàn)略聯(lián)盟的動因與模式選擇研究.pdf
- c 設(shè)計模式工廠型設(shè)計示例
- 如何創(chuàng)建企業(yè)的培訓(xùn)體系
- 三型支部創(chuàng)建方案
評論
0/150
提交評論