Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE 461 Software Patterns. ABSTRACT FACTORY PATTERN.

Similar presentations


Presentation on theme: "SE 461 Software Patterns. ABSTRACT FACTORY PATTERN."— Presentation transcript:

1 SE 461 Software Patterns

2 ABSTRACT FACTORY PATTERN

3 Differences in ingrdients: -Franchises use different ingredients, -Souce, cheese… are different in regions, -You want to control the use of these ingredients in franchises, -How?

4

5 Building the ingredient factories

6 public interface PizzaIngredientFactory { public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); public Veggies[] createVeggies(); public Pepperoni createPepperoni(); public Clans createClans(); }

7 Here’s what we are going to do: 1- Build a factory for each region. 2- Implement a set of ingredient classes to be used with the factory, like ReggianoCheese, RedPeppers… These classes can be shared among regions where appropriate. 3- Then we still need to hook all this up by working our new ingredient factories into our old PizzaStore code.

8 public class NYPizzaIngredientFactory implements PizzaIngredientFactory { public Dough createDough() { return new ThinCrustDough(); } //implement all other create methods… }

9 Reworking the pizzas… Rework on pizzas to use factory produced ingredients.

10 public abstract class Pizza { String name, dough, sauce; Veggies veggies[]; Cheese cheese; Pepperoni pepperoni; Clans clan; abstract vois prepare(); //other are same }

11 Reworking the pizzas… (cont.) Create NY and Chicago style pizzas. They will get ingredients from the factory.

12 public class CheesePizza extends Pizza { PizzaIngredientFactory ingredientFactory; public CheesePizza(PizzaIngredientFactory ingredientFactory) { this.ingreidentFactory = ingredientFactory; } void prepare() { System.out.println(“Prepare “ + name); dough = ingredientFactory.createDough(); sauce= ingredientFactory.createSauce(); cheese= ingredientFactory.createCheese(); }

13 public class NYPizzaStore extends PizzaStore { protected Pizza createPizza(String item) { Pizza pizza = null; PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); if(item.equals(“cheese”)) { pizza = new CheesePizza(ingredientFactory); pizza.setName(“New York Style Cheese Pizza”); } //similar for other types.. return pizza; }

14 What happens when you order a pizza

15

16

17

18 The Abstract Factory Pattern privides an interface for creating families of related or dependent objects without specifying their concrete classes.

19

20

21

22 Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Factory Method Defines an interface for creating an object but lets subclasses decide which class to instantiate.

23 References


Download ppt "SE 461 Software Patterns. ABSTRACT FACTORY PATTERN."

Similar presentations


Ads by Google