1 / 25

Factory Pattern

Factory Pattern. Building Complex Objects. New is an implementation. Calling “new” is certainly coding to an implementation In fact, it’s always related to a concrete class (Open for Modification) That’s fine when things are simple, but. Look Out For Change.

les
Télécharger la présentation

Factory Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Factory Pattern Building Complex Objects

  2. New is an implementation • Calling “new” is certainly coding to an implementation • In fact, it’s always related to a concrete class • (Open for Modification) • That’s fine when things are simple, but . . .

  3. Look Out For Change • When you have several related classes, that’s probably a good sign that they might change in the future Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new DecoyDuck(); } else if (inBathTub) { duck = new RubberDucky(); }

  4. Design Principle • What should you try to do with code that changes?

  5. When Building Objects is Complex • Book example - a variety of types of pizzas • Think about constructing the weapons in your decorator lab

  6. Pizza orderPizza() { Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

  7. Pizza orderPizza(String type) { Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if (type.equals(“greek”)) { pizza = new GreekPizza(); } else if (type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

  8. Pizza orderPizza(String type) { Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if (type.equals(“greek”)) { pizza = new GreekPizza(); } else if (type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } else if (type.equals(“sausage”)) { pizza = new SausagePizza(); } else if (type.equals(“veggie”)) { pizza = new VeggiePizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

  9. Pizza orderPizza(String type) { Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if (type.equals(“greek”)) { pizza = new GreekPizza(); } else if (type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } else if (type.equals(“sausage”)) { pizza = new SausagePizza(); } else if (type.equals(“veggie”)) { pizza = new VeggiePizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } Encapsulate!

  10. public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if (type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } else if (type.equals(“sausage”)) { pizza = new SausagePizza(); } else if (type.equals(“veggie”)) { pizza = new VeggiePizza(); } return pizza; } } Now orderPizza() is tidy

  11. public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cute(); pizza.box(); return pizza; } } No new operator

  12. Simple Factory • Pull the code that builds the instances out and put it into a separate class • Identify the aspects of your application that vary and separate them from what stays the same

  13. Simple Factory Pattern Client orderProduct() SimpleFactory createProduct() <<abstract>> Product productMethod() ConcreteProductA ConcreteProductB ConcreteProductC

  14. Why would we do this? • Multiple clients needing same types of object • Ensure consistent object initialization

  15. The Factory Method Definition: The factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

  16. Factory Method Pattern <<abstract>> Creator factoryMethod() operation() <<abstract>> Product abstract! IS-A IS-A HAS-A ConcreteProduct ConcreteCreator factoryMethod() No more SimpleFactory class Object creation is back in our class, but … delegated to concrete classes

  17. Dependency Inversion Principle Depend upon abstractions -- not concrete classes

  18. Impossible Guidelines • No variable should hold a reference to a concrete class • No class should derive from a concrete class • No method should override an implemented method of an of its base classes.

  19. Impossible Guidelines The idea here is to stay clear of classes that are likely to change String studentChecking System businessChecking Integer FederalTaxCode

  20. What if there are categories of Products? <<abstract>> Player HeroGunner EnemyLevelBoss HeroStealth EnemyCaptain HeroTank EnemyThug HeroPilot

  21. When Subclasses get out of hand… • make the factory an abstract class as well • Instead of subclasses for the products, subclasses for the components of the products • The next diagram is overwhelming… • breathe in, breath out

  22. <<interface>> AbstractFactory createProductA() createProductB() Client <<interface>> AbstractProductA <<interface>> AbstractProductB ConcreteFactory1 createProductA() createProductB() ConcreteProductA1 ConcreteFactory2 createProductA() createProductB() ConcreteProductA2 ConcreteProductB1 ConcreteProductB2 Abstract Factory Pattern

  23. Abstract Factory Pattern This design favors composition handy when you need to insure the quality of many similar, but slightly different versions of a class

  24. More Diagrams • pp.156/157 • 160/161

  25. Summary • Simple Factory • Use when you have only one family of objects • Factory Method Pattern • Use when you have multiple families of objects • Abstract Factory • Use when you have multiple families of object components

More Related