Factory and Abstract Factory


Published on

This is Class 2 on a 6 week course I taught on Software Design Patterns.

This course goes over Simple Factory, Factory Method, and Abstract Factory.

Class based on "Head First Design Patterns."

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 13 November 2008
  • 13 November 2008
  • 13 November 2008
  • Singleton Template Strategy 13 November 2008
  • 13 November 2008
  • 13 November 2008
  • Remove the abstract keyword 13 November 2008
  • 13 November 2008
  • Factory and Abstract Factory

    1. 1. Design Patterns 05/28/10 Week 3: Factory Method and Abstract Factory Jonathan Simon [email_address]
    2. 2. Agenda <ul><li>Tangent : Preventing Future Mistakes </li></ul><ul><li>Simple Factory </li></ul><ul><li>Factory Method </li></ul><ul><li>Abstract Factory </li></ul>05/28/10
    3. 3. Head First Webpage <ul><li>http://www.headfirstlabs.com/books/hfdp/ </li></ul><ul><li>Contains code examples from the book in Java and C# </li></ul><ul><li>Forums Link for this book: </li></ul><ul><ul><li>http://forums.oreilly.com/category/48/Head-First-Design-Patterns/ </li></ul></ul>05/28/10
    4. 4. Tangent : Preventing Future Mistakes <ul><li>Imagine if we had code like this: </li></ul><ul><ul><li>public class MySuperClass { </li></ul></ul><ul><ul><li>public void DoSomething() { </li></ul></ul><ul><ul><li> //step 1 </li></ul></ul><ul><ul><li>//step 2 </li></ul></ul><ul><ul><li> .. </li></ul></ul><ul><ul><li>//step 6 </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Developer A needs to slightly change steps 2 and 6 for a certain case. </li></ul><ul><li>Developer creates a new class, overrides DoSomething(), and then copies and paste the entire function contents. </li></ul>05/28/10
    5. 5. Preventing Future Mistakes (cont) <ul><ul><li>public class MySuperClass { </li></ul></ul><ul><ul><li>public void DoSomething() { </li></ul></ul><ul><ul><li> //step 1 </li></ul></ul><ul><ul><li>//step 2 </li></ul></ul><ul><ul><li> .. </li></ul></ul><ul><ul><li>//step 5 </li></ul></ul><ul><ul><li>//step 6 </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public class SubClass extends MySuperClass { </li></ul></ul><ul><ul><li>public void DoSomething() { </li></ul></ul><ul><ul><li> //step 1 Copy </li></ul></ul><ul><ul><li>//step 2 Copy with Slight Change </li></ul></ul><ul><ul><li> . . . </li></ul></ul><ul><ul><li> //step 5 Copy </li></ul></ul><ul><ul><li>//step 6 Copy with Slight Change </li></ul></ul><ul><ul><li>} </li></ul></ul>05/28/10 The method DoSomething() is overwriting behavior in the superclass.
    6. 6. Preventing Future Mistakes (cont) <ul><li>Developer A leaves the company. </li></ul><ul><li>Developer B joins the company. </li></ul><ul><li>A bug is found in Step 2 of the algorithm. Developer B makes the fix in MySuperClass and the client is told that the problem has been fixed. </li></ul><ul><li>But months later the bug reappears in SubClass. The client is not happy. </li></ul>05/28/10
    7. 7. Preventing Future Mistakes (cont) <ul><li>If Developer A had used the Template Pattern, it would have minimized this problem from occurring. </li></ul><ul><li>Realities: </li></ul><ul><li>Developer A may have been pressed for time. </li></ul><ul><li>Developer A should have put comments in the code that refactoring is needed. </li></ul><ul><li>Developer A should have coordinated with Manager to indicate that this refactoring should be added to a list and prioritized. </li></ul><ul><li>Developer A had no idea that they were doing something wrong! </li></ul><ul><li>This is a good example of Technical Debt! </li></ul>05/28/10
    8. 8. Technical Debt 05/28/10 Source: http://martinfowler.com/bliki/TechnicalDebtQuadrant.html
    9. 9. Review: What Pattern should we consider? <ul><li>We need to manage a shared network printer. </li></ul><ul><li>We create an algorithm for renting movies online. MoviesOnline.com and ObjectvilleMovies both use the exact same algorithm, but MoviesOnline.com would like the additional capability of displaying the next movie in the queue after a movie is rented. </li></ul><ul><li>You are creating a character for a computer game. You want the character to be able to support different victory dances. The computer should be able to assign different victory dances during game play. </li></ul>05/28/10
    10. 10. Today’s Class <ul><li>Word of the Day = Factory </li></ul><ul><li>Different Flavors </li></ul><ul><ul><li>Simple Factory </li></ul></ul><ul><ul><li>Factory Method </li></ul></ul><ul><ul><li>Abstract Factory </li></ul></ul>05/28/10
    11. 11. Pizza Store in Objectville <ul><li>We need a pizza store that can create pizza. </li></ul><ul><li>The customer will order a specific type of pizza: </li></ul><ul><ul><li>Cheese </li></ul></ul><ul><ul><li>Veggie </li></ul></ul><ul><ul><li>Greek </li></ul></ul><ul><ul><li>Pepperoni </li></ul></ul><ul><ul><li>etc </li></ul></ul><ul><li>Each order request is for one type of pizza only. </li></ul>05/28/10
    12. 12. PizzaStore in Objectville <ul><li>During the ordering of a Pizza, we need to perform certain actions on it: </li></ul><ul><ul><li>Prepare </li></ul></ul><ul><ul><li>Bake </li></ul></ul><ul><ul><li>Cut </li></ul></ul><ul><ul><li>Box </li></ul></ul><ul><li>We know that all Pizzas must perform these behaviors. In addition, we know that these behaviors will not change during runtime . (i.e. the Baking time for a Cheese Pizza will never change!) </li></ul><ul><li>Question : Should these behaviors (prepare, bake, etc) be represented using Inheritance or Composition? </li></ul>05/28/10
    13. 13. PizzaStore in Objectville (pg 112 top) <ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza = new Pizza(); </li></ul><ul><li>pizza.prepare(); </li></ul><ul><li>pizza.bake(); </li></ul><ul><li>pizza.cut(); </li></ul><ul><li>pizza.box(); </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul><ul><li>Creating an instance of Pizza() doesn’t make sense here because we know there are different types of Pizza. </li></ul>05/28/10 This method is responsible for creating the pizza. It calls methods to prepare, bake, etc. Pizza is returned to caller.
    14. 14. PizzaStore in Objectville (pg 112 middle) <ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>if (type.equals(&quot;cheese&quot;)) { </li></ul><ul><li>pizza = new CheesePizza(); </li></ul><ul><li>} else if (type.equals(&quot;pepperoni&quot;)) { </li></ul><ul><li>pizza = new PepperoniPizza(); </li></ul><ul><li>pizza.prepare(); </li></ul><ul><li>pizza.bake(); </li></ul><ul><li>pizza.cut(); </li></ul><ul><li>pizza.box(); </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul>05/28/10 Code that varies A parameter indicating type Code that stays the same
    15. 15. PizzaStore in Objectville (cont) <ul><li>pg 113. Pressure is on for change… </li></ul><ul><li>Now we get some new types of Pizza (Clam, Veggie) </li></ul><ul><li>Every time there is a change, we would need to break into this code and update the If/Else statement. (and possibly introduce bugs in our existing code). </li></ul>05/28/10
    16. 16. Solution? (pg 115) <ul><li>Move the creation of Pizzas into a separate object! </li></ul><ul><li>public class SimplePizzaFactory { </li></ul><ul><li>public Pizza createPizza(String type) { </li></ul><ul><li>Pizza pizza = null; </li></ul><ul><li>if (type.equals(&quot;cheese&quot;)) { </li></ul><ul><li>pizza = new CheesePizza(); </li></ul><ul><li>} else if (type.equals(&quot;pepperoni&quot;)) { </li></ul><ul><li>pizza = new PepperoniPizza(); </li></ul><ul><li>} else if (type.equals(&quot;clam&quot;)) { </li></ul><ul><li>pizza = new ClamPizza(); </li></ul><ul><li>} else if (type.equals(&quot;veggie&quot;)) { </li></ul><ul><li>pizza = new VeggiePizza(); </li></ul><ul><li>} </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10
    17. 17. SimplePizzaFactory <ul><li>Advantage: We have one place to go to add a new pizza. </li></ul><ul><li>Disadvantage: Whenever there is a change, we need to break into this code and add a new line. (but at least it is in one place!!) </li></ul>05/28/10
    18. 18. Rework of PizzaStore (pg 116) <ul><li>public class PizzaStore { </li></ul><ul><li>SimplePizzaFactory factory ; </li></ul><ul><li>public PizzaStore( SimplePizzaFactory factory ) { </li></ul><ul><li>this.factory = factory; </li></ul><ul><li>} </li></ul><ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza; </li></ul><ul><li>pizza = factory.createPizza(type); </li></ul><ul><li>pizza.prepare(); </li></ul><ul><li>pizza.bake(); </li></ul><ul><li>pizza.cut(); </li></ul><ul><li>pizza.box(); </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Store is composed of a factory. Creation of pizza is delegated to factory.
    19. 19. Simple Factory Defined (pg 117) <ul><li>This is not an official pattern, but it is commonly used. </li></ul><ul><li>Not a bad place to start. </li></ul><ul><li>When people think of “Factory”, they may actually be thinking of this. </li></ul><ul><ul><li>Important: In a team discussion, you may need to define your vocabulary. </li></ul></ul>05/28/10
    20. 20. Change Now Occurs… <ul><li>The PizzaStore is very popular and it needs to be franchised. </li></ul><ul><ul><li>New York is interested </li></ul></ul><ul><ul><li>Chicago is interested </li></ul></ul><ul><ul><li>And perhaps one day Fairfax… </li></ul></ul><ul><li>Since PizzaStore is already composed of a Simple Factory, then this should be easy! Let’s just create PizzaStore with a different SimpleFactory. </li></ul>05/28/10
    21. 21. Example (pg 119) <ul><li>//NY Pizza Factory has a different if/else logic </li></ul><ul><li>NYPizzaFactory nyFactory = new NYPizzaFactory(); </li></ul><ul><li>//Create the Pizza Store, but use this Simple Factory </li></ul><ul><li>//instead </li></ul><ul><li>PizzaStore nyStore = new PizzaStore(nyFactory); </li></ul><ul><li>//Order pizza </li></ul><ul><li>nyStore.order(“Veggie”); </li></ul>05/28/10
    22. 22. More change happens… <ul><li>New York likes the PizzaStore, but they want to add more functionality, such as schedule a delivery. </li></ul><ul><li>New York attempts to extend PizzaStore… </li></ul><ul><ul><li>public class NYPizzaStore extends PizzaStore { </li></ul></ul><ul><ul><li>public void ScheduleDelivery() { </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>05/28/10
    23. 23. More change happens… (cont) <ul><li>NYPizzaFactory nyFactory = new NYPizzaFactory(); </li></ul><ul><li>NYPizzaStore nyStore = new NYPizzaStore( nyFactory ); </li></ul><ul><li>//Order pizza </li></ul><ul><li>nyStore.order(“Veggie”); </li></ul><ul><li>myStore.ScheduleDelivery(); </li></ul><ul><li>New York says the following: </li></ul><ul><ul><li>We only have one way to create pizzas; therefore, we don’t need to use composition for the pizza creation. </li></ul></ul><ul><ul><li>We are not happy that we have to create our extended Pizza store and create a unique factory for creating pizzas. These two classes have a one-to-one relationship with each other. Can’t they be combined?? </li></ul></ul>05/28/10
    24. 24. What New York wants <ul><li>A framework so that NY can do the following: </li></ul><ul><li>Create pizzas in a NY style </li></ul><ul><li>Add additional functionality that is applicable to NY only. </li></ul>05/28/10
    25. 25. A Framework for Pizza Store (pg 120) <ul><li>public abstract class PizzaStore { </li></ul><ul><li>abstract Pizza createPizza(String item); </li></ul><ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza = createPizza(type); </li></ul><ul><li>pizza.prepare(); </li></ul><ul><li>. . . </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 NOTE : We are using inheritance here to create the pizzas, not composition. Also, the constructor for PizzaStore has been removed.
    26. 26. NYPizzaStore (pg 123) <ul><li>public class NYPizzaStore extends PizzaStore { </li></ul><ul><ul><li>Pizza createPizza(String item) { </li></ul></ul><ul><ul><ul><li>if (item.equals(&quot;cheese&quot;)) { </li></ul></ul></ul><ul><ul><ul><li>return new NYStyleCheesePizza(); </li></ul></ul></ul><ul><ul><ul><li>} else if (item.equals(&quot;veggie&quot;)) { </li></ul></ul></ul><ul><ul><ul><li>return new NYStyleVeggiePizza(); </li></ul></ul></ul><ul><ul><ul><li>} else if (item.equals(&quot;clam&quot;)) { </li></ul></ul></ul><ul><ul><ul><li>return new NYStyleClamPizza(); </li></ul></ul></ul><ul><ul><ul><li>} else if (item.equals(&quot;pepperoni&quot;)) { </li></ul></ul></ul><ul><ul><ul><li>return new NYStylePepperoniPizza(); </li></ul></ul></ul><ul><ul><ul><li>} else return null; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>void ScheduleDelivery(); </li></ul></ul><ul><li>} </li></ul>05/28/10 The subclass is defining how the pizza is created….and it is also providing unique functionality that is applicable to New York.
    27. 27. Factory Method <ul><li>public abstract class PizzaStore { </li></ul><ul><li>abstract Pizza createPizza(String item); </li></ul><ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza = createPizza(type); </li></ul><ul><li>pizza.prepare(); </li></ul><ul><li>. . . </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Factory Method simply sets up an interface for creating a Product (in this case, a type of Pizza). Subclasses decide which specific Product to create.
    28. 28. Test Drive (pg 127) <ul><li>1) PizzaStore nyStore = new NYPizzaStore(); </li></ul><ul><li>2) nyStore.orderPizza(&quot;cheese&quot;); </li></ul><ul><li>3)Calls createPizza(“cheese”). The NYPizzaStore version of createPizza is called. It returns a NYStyleCheesePizza(); </li></ul><ul><li>4) orderPizza continues to call the “parts that do not vary”, such as prepare, bake, cut, box. </li></ul>05/28/10
    29. 29. Factory Method Defined <ul><li>GoF Intent: “Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.” </li></ul>05/28/10
    30. 30. Definitions (pg 134) <ul><li>Product - abstraction of the object that we need to create (Pizza) </li></ul><ul><li>Concrete Product – implementations of a specific Product (NYStyleCheesePizza) </li></ul><ul><li>Creator – abstraction of the object that will create a Product (PizzaStore). Contains factory method. </li></ul><ul><li>Concrete Creator – implementation of a Creator that creates a specific ConcreteProduct (NYPizzaStore) </li></ul>05/28/10
    31. 31. IMPORTANT <ul><li>The Factory Method should just return a ConcreteProduct. </li></ul>05/28/10
    32. 32. Lab <ul><li>public abstract class PizzaStore { </li></ul><ul><li>abstract Pizza createPizza(String item); </li></ul><ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza = createPizza(type); </li></ul><ul><li>. . . </li></ul><ul><li>return pizza; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 <ul><li>Did we separate out the parts that vary from the parts that do not vary? </li></ul><ul><li>Did we Program to an Interface, not an Implementation? </li></ul><ul><li>Does the Hollywood Principle apply here? </li></ul><ul><li>Did we favor composition over inheritance? </li></ul>
    33. 33. Parameterized Factory Method <ul><li>The example from the book is actually a Parameterized Factory Method since it can make more than one object (based on some input parameter). </li></ul><ul><li>abstract Pizza createPizza(String item); </li></ul><ul><li>It may have been a little easier if the book had started with a simpler Factory Method example… </li></ul>05/28/10
    34. 34. Simple Factory Method <ul><li>public abstract class PizzaStore { </li></ul><ul><li>abstract Pizza createPizza(); //no parameter! </li></ul><ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza = createPizza(type); </li></ul><ul><li>. . . </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public class NYPizzaStore extends PizzaStore { </li></ul><ul><li>Pizza createPizza() { </li></ul><ul><li>return new NYStylePizza(); //just one product returned </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10
    35. 35. Simple Factory Method (with Default) <ul><li>public abstract class PizzaStore { </li></ul><ul><li>void Pizza createPizza() { </li></ul><ul><li>return new DefaultPizza(); </li></ul><ul><li>} </li></ul><ul><li>public Pizza orderPizza(String type) { </li></ul><ul><li>Pizza pizza = createPizza(type); </li></ul><ul><li>. . . </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 In this example, there is default Product created (DefaultPizza).
    36. 36. Back to SimUDuck (pg 16) <ul><li>public class MallardDuck extends Duck { </li></ul><ul><li>public MallardDuck() { </li></ul><ul><li> quackBehavior = new Quack(); </li></ul><ul><li>flyBehavior = new FlyWithWings(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Developer would have to know to do this. The design does not enforce it. MallardDuck is tied to two specific implementations.
    37. 37. Before <ul><li>public abstract class Duck { </li></ul><ul><li>FlyBehavior flyBehavior; </li></ul><ul><li> QuackBehavior quackBehavior; </li></ul><ul><li>public Duck() { </li></ul><ul><li>} </li></ul><ul><li>//Not shown: setFlyBehavior, setQuackBehavior </li></ul><ul><li>//Not shown: abstract display method, fly, quack </li></ul><ul><li>} </li></ul>05/28/10
    38. 38. After <ul><li>public abstract class Duck { </li></ul><ul><li> FlyBehavior flyBehavior; </li></ul><ul><li> QuackBehavior quackBehavior; </li></ul><ul><li>abstract FlyBehavior createFly(); </li></ul><ul><li>abstract QuackBehavior createQuack(); </li></ul><ul><li> public Duck() { </li></ul><ul><li>flyBehavior = createFly(); </li></ul><ul><li>quackBehavior = createQuack(); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>05/28/10 Factory Methods
    39. 39. Now we create MallardDuck… <ul><li>public class MallardDuck extends Duck { </li></ul><ul><li>public MallardDuck() { </li></ul><ul><li>} </li></ul><ul><li>FlyBehavior createFly() { </li></ul><ul><li>return FlyWithWings(); </li></ul><ul><li>} </li></ul><ul><li>QuackBehavior createQuack() { </li></ul><ul><li>return Quack(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Developer is “forced” to specify implementations of Fly and Quack. Note that the constructor is empty.
    40. 40. Alternative Design: Default Behavior <ul><li>public abstract class Duck { </li></ul><ul><li> FlyBehavior flyBehavior; </li></ul><ul><li> QuackBehavior quackBehavior; </li></ul><ul><li>FlyBehavior createFly() { </li></ul><ul><li>return DefaultFly(); </li></ul><ul><li>} </li></ul><ul><li>QuackBehavior createQuack() { </li></ul><ul><li>return DefaultQuack(); </li></ul><ul><li>} </li></ul><ul><li> </li></ul><ul><li>public Duck() { </li></ul><ul><li>flyBehavior = createFly(); </li></ul><ul><li>quackBehavior = createQuack(); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>05/28/10 MallardDuck does not have to implement these methods.
    41. 41. Factory Method and Template <ul><li>public abstract class CaffeineBeverage { </li></ul><ul><li>final void prepareRecipe() { </li></ul><ul><li>boilWater(); </li></ul><ul><li>brew(); </li></ul><ul><li>pourInCup(); </li></ul><ul><li>Condiment c = createCondiment(); </li></ul><ul><li>addCondiments(c); </li></ul><ul><li>} </li></ul><ul><li>abstract void brew(); </li></ul><ul><li>abstract Condiment createCondiment(); </li></ul><ul><li>void addCondiments(Condiment c) { </li></ul><ul><li>//add condiment c </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Example re-worked here
    42. 42. Summary <ul><li>Pattern Name – Factory Method </li></ul><ul><li>Problem – Do not want our framework to be tied to a specific implementation of an object. </li></ul><ul><li>Solution </li></ul><ul><ul><li>Let subclass decide which implementation to use (via use of an abstract method) </li></ul></ul><ul><ul><li>Tie the Creator with a specific Product Implementation </li></ul></ul><ul><li>Consequences </li></ul><ul><ul><li>Simple Factory Method </li></ul></ul><ul><ul><li>Simple Factory Method with Default Behavior </li></ul></ul><ul><ul><li>Parameterized Factory Method </li></ul></ul><ul><ul><li>Factory and Template Together </li></ul></ul>05/28/10
    43. 43. Changes.. <ul><li>Objectville Pizza HQ gets complaints that the different franchises are not using quality ingredients. They decide that all Pizzas must be composed of the following ingredients: </li></ul><ul><ul><li>Dough, Sauce, Cheese, Veggies, Pepperoni, and Clams. </li></ul></ul><ul><li>New York says those ingredients are fine, but they want to use their own regional ingredients for all types of pizza, for example </li></ul><ul><ul><li>Dough = Thin Crust Dough </li></ul></ul><ul><ul><li>Sauce = Marinara </li></ul></ul><ul><ul><li>Cheese = Reggiano </li></ul></ul><ul><ul><li>Veggies = Garlic, Onion </li></ul></ul>05/28/10
    44. 44. Pizza <ul><li>public class Pizza { </li></ul><ul><li>Dough dough; </li></ul><ul><li>Sauce sauce; </li></ul><ul><li>Veggies veggies[]; </li></ul><ul><li>Cheese cheese; </li></ul><ul><li>Pepperoni pepperoni; </li></ul><ul><li>Clams clam; </li></ul><ul><li>. . . </li></ul><ul><li>} </li></ul>05/28/10 Pizza becomes composed of different ingredients. Question: How do we get these ingredients associated with a Pizza??
    45. 45. One idea…Constructor w/many parameters <ul><li>public class Pizza { </li></ul><ul><li>public Pizza( Dough d, Sauce s, Cheese c, Veggies v, </li></ul><ul><li>Pepperoni p, Clams c ) { </li></ul><ul><li>this.dough = d; </li></ul><ul><li>this.sauce = s; </li></ul><ul><li>this.veggies = v; </li></ul><ul><li>this.cheese = c; </li></ul><ul><li>this.pepperoni = p; </li></ul><ul><li>this.clam = c; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Makes sense…whenever you create a pizza, you must specify these ingredients…
    46. 46. Rework NYPizzaStore <ul><li>public class NYPizzaStore extends PizzaStore { </li></ul><ul><ul><li>Pizza createPizza(String item) { </li></ul></ul><ul><ul><ul><li>if (item.equals(&quot;cheese&quot;)) { </li></ul></ul></ul><ul><ul><ul><li>return new NYStyleCheesePizza(new ThinCrustDough(), </li></ul></ul></ul><ul><ul><ul><li>new Marinara(), new Reggiano(), null, null ); </li></ul></ul></ul><ul><ul><ul><li>} else if (item.equals(&quot;veggie&quot;)) { </li></ul></ul></ul><ul><ul><ul><li>return new NYStyleVeggiaPizza(new ThinCrustDough(), </li></ul></ul></ul><ul><ul><ul><li>new Marinara(), new Reggiano(), mew Garlic() , null) </li></ul></ul></ul><ul><ul><li> } </li></ul></ul><ul><li>} </li></ul>05/28/10 This will cause a lot of maintenance headaches!!! Imagine what happens when we create a new pizza! We know that we have a certain set of ingredients that are used for New York..yet we have to keep repeating that set with each constructor. Can we define this unique set just once??
    47. 47. PizzaIngredientFactory (pg 146) <ul><li>public interface PizzaIngredientFactory { </li></ul><ul><li>public Dough createDough(); </li></ul><ul><li>public Sauce createSauce(); </li></ul><ul><li>public Cheese createCheese(); </li></ul><ul><li>public Veggies[] createVeggies(); </li></ul><ul><li>public Pepperoni createPepperoni(); </li></ul><ul><li>public Clams createClam(); </li></ul><ul><li>} </li></ul>05/28/10 Create an interface for creating the different ingredients of Pizza. Note that each ingredient of Pizza has a corresponding method defined in the interface.
    48. 48. NYPizzaIngredientFactory (pg 147) <ul><li>public class NYPizzaIngredientFactory implements PizzaIngredientFactory { </li></ul><ul><li>public Dough createDough() { </li></ul><ul><li>return new ThinCrustDough(); </li></ul><ul><li>} </li></ul><ul><li>public Sauce createSauce() { </li></ul><ul><li>return new MarinaraSauce(); </li></ul><ul><li>} </li></ul><ul><li>public Cheese createCheese() { </li></ul><ul><li>return new ReggianoCheese(); </li></ul><ul><li>} </li></ul><ul><li>. . . </li></ul><ul><li>} </li></ul>05/28/10 Here we see the set of ingredients that are specific to the NY region. Note that each time a createXXX() method is called, a new instance is returned. This will be important later on.
    49. 49. CheesePizza (pg 150) <ul><li>public class CheesePizza extends Pizza { </li></ul><ul><li>PizzaIngredientFactory ingredientFactory; </li></ul><ul><li>public CheesePizza( PizzaIngredientFactory ingredientFactory ) { </li></ul><ul><li>this.ingredientFactory = ingredientFactory; </li></ul><ul><li>} </li></ul><ul><li>void prepare() { </li></ul><ul><li>dough = ingredientFactory.createDough(); </li></ul><ul><li>sauce = ingredientFactory.createSauce(); </li></ul><ul><li>cheese = ingredientFactory.createCheese(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>05/28/10 Instead of many ingredient parameters, we just pass one. Does this remind you of another pattern?? Creation delegated Note: Not all ingredients have to be used!
    50. 50. NYPizzaStore (pg 152) <ul><li>public class NYPizzaStore extends PizzaStore { </li></ul><ul><li>protected Pizza createPizza(String item) { </li></ul><ul><li>Pizza pizza = null; </li></ul><ul><li>PizzaIngredientFactory ingredientFactory = </li></ul><ul><li>new NYPizzaIngredientFactory(); </li></ul><ul><li>if (item.equals(&quot;cheese&quot;)) { </li></ul><ul><li>pizza = new CheesePizza(ingredientFactory); </li></ul><ul><li>} else if (item.equals(&quot;veggie&quot;)) { </li></ul><ul><li>pizza = new VeggiePizza(ingredientFactory); </li></ul><ul><li>} else if (item.equals(&quot;clam&quot;)) { </li></ul><ul><li>pizza = new ClamPizza(ingredientFactory); </li></ul><ul><li>} else if (item.equals(&quot;pepperoni&quot;)) { </li></ul><ul><li>pizza = new PepperoniPizza(ingredientFactory); </li></ul><ul><li>} </li></ul><ul><li>return pizza; </li></ul>05/28/10 The specific ingredient factory is defined once for the NY region.
    51. 51. Abstract Factory Defined <ul><li>GoF Intent: “Provides an interface for creating families of related or dependent objects without specifying their concrete classes.” </li></ul>05/28/10
    52. 52. Factory Method vs. Abstract Factory <ul><li>Factory Method </li></ul><ul><ul><li>Uses inheritance to create a Concrete Product </li></ul></ul><ul><ul><li>Sub classes decide which Concrete Product to use </li></ul></ul><ul><li>Abstract Factory </li></ul><ul><ul><li>Uses composition to create objects </li></ul></ul><ul><ul><li>The objects created were a part of a family of objects. For example, NY region had a specific set of ingredients. </li></ul></ul><ul><ul><li>An abstract factory actually contains one or more Factory Methods! </li></ul></ul>05/28/10
    53. 53. Database Example <ul><li>Imagine you wanted to created a database manager object as an enterprise component. This object must not be tied to a specific database implementation. </li></ul><ul><li>Each specific database uses different objects: </li></ul><ul><ul><li>Connection </li></ul></ul><ul><ul><li>Command </li></ul></ul><ul><ul><li>Adapter </li></ul></ul><ul><ul><li>Data Reader </li></ul></ul><ul><li>Oracle, SQL Server, Informix, etc will have their own version of these Sub Products. </li></ul>05/28/10
    54. 54. DatabaseFactory <ul><li>public abstract class DatabaseFactory </li></ul><ul><li>{ </li></ul><ul><ul><li>public abstract IDbCommand CreateCommand(); </li></ul></ul><ul><ul><li>public abstract IDbCommand CreateCommand(string cmdText); </li></ul></ul><ul><ul><li>public abstract IDbCommand CreateCommand(string cmdText, IDbConnection cn); </li></ul></ul><ul><ul><li>public abstract IDbConnection CreateConnection(); </li></ul></ul><ul><ul><li>public abstract IDbConnection CreateConnection(string cnString); </li></ul></ul><ul><ul><li>public abstract IDbDataAdapter CreateDataAdapter(); </li></ul></ul><ul><ul><li>public abstract IDbDataAdapter CreateDataAdapter(IDbCommand selectCmd); </li></ul></ul><ul><ul><li>public abstract IDataReader CreateDataReader(IDbCommand dbCmd); </li></ul></ul><ul><li>} </li></ul>05/28/10 Each database implementation (Oracle, SQL Server, etc) will need to create their own version of this DatabaseFactory. The specific DatabaseFactory must be sent to the constructor of the framework class DatabaseManager.
    55. 55. Abstract Factory and Singleton <ul><li>If you have many different Abstract Factories, you probably just need one instance of each abstract factory. </li></ul><ul><li>You can use a Singleton to define each Abstract Factory. </li></ul>05/28/10
    56. 56. A more interesting case… <ul><li>What if NY Pizza Store wanted the flexibility to change their ingredients all the time?? </li></ul><ul><li>For example, one day use: </li></ul><ul><ul><li>ThickCrust Dough (not Thin) </li></ul></ul><ul><ul><li>Marina Sauce (which was previously used) </li></ul></ul><ul><ul><li>Provolone Cheese (not Reggiano) </li></ul></ul><ul><li>And then next week the cheese needs to be changed to Cheddar (since Provolone shipment didn’t come in time). </li></ul>05/28/10
    57. 57. What will happen?? <ul><li>Class Explosion! </li></ul><ul><li>We would need an Abstract Factory for every possible combination…and when we add a new ingredient, we could have to create even more Abstract Factories. </li></ul>05/28/10
    58. 58. Solution: Abstract Factory and Prototype <ul><li>When we create the abstract factory, pass the different applicable products in the constructor. </li></ul><ul><li>PizzaIngredientFactory ingredientFactory = </li></ul><ul><li>new NYPizzaIngredientFactory </li></ul><ul><li>( new ThinCrustDough(), new MarinaraSauce(), </li></ul><ul><li> new Provolone() ); </li></ul><ul><li>These products could easily come from an XML file or even by the user during runtime. </li></ul>05/28/10
    59. 59. Solution: Abstract Factory and Prototype <ul><li>The abstract factory can then be passed to the different types of Pizzas. </li></ul><ul><li>When a method is called (ie, createDough, createSauce), a cloned copy of the Product is returned. </li></ul><ul><ul><li>This ensures that each call to a createXXX method returns a unique instance. </li></ul></ul><ul><ul><li>Note: In our standard abstract factory, a unique instance is returned every time you call a createXXX method. </li></ul></ul><ul><li>More information on Prototype </li></ul><ul><ul><li>http://www.oodesign.com/prototype-pattern.html </li></ul></ul><ul><ul><li>Gang of Four (pg 122) </li></ul></ul>05/28/10
    60. 60. Abstract Factory: Last Thoughts <ul><li>This may be a hard pattern to “see” during the initial design process. </li></ul><ul><li>You may encounter this pattern during refactoring. </li></ul>05/28/10
    61. 61. Summary <ul><li>Pattern Name – Abstract Factory </li></ul><ul><li>Problem – Need to way to create a family of products </li></ul><ul><li>Solution </li></ul><ul><ul><li>Create an interface for creating products </li></ul></ul><ul><li>Consequences </li></ul><ul><ul><li>Objects are created using interface </li></ul></ul><ul><ul><li>Composition is used to create objects </li></ul></ul><ul><ul><li>Abstract Factory and Singleton </li></ul></ul><ul><ul><li>Abstract Factory and Prototype </li></ul></ul>05/28/10