Day05_AbstractFactory
Day05_AbstractFactory
9/7/2016 1
Pizza
public class Pizza {
Dough dough;
Sauce sauce;
Pizza becomes composed of
Veggies veggies[];
different ingredients.
Cheese cheese;
Pepperoni pepperoni;
Clams clam;
Question: How do we
. . . get these ingredients
associated with a
} Pizza??
9/7/2016 2
One idea…Constructor w/many
parameters
public class Pizza {
this.dough = d;
this.sauce = s;
this.veggies = v;
this.cheese = c; Makes sense…whenever
this.pepperoni = p; you create a pizza, you
must specify these
this.clam = c;
ingredients…
}
}
9/7/2016 3
Rework NYPizzaStore
public class NYPizzaStore extends PizzaStore {
if (item.equals("cheese")) {
return new NYStyleCheesePizza(new ThinCrustDough(),
new Marinara(), new Reggiano(), null, null );
} else if (item.equals("veggie")) {
return new NYStyleVeggiaPizza(new ThinCrustDough(),
new Marinara(), new Reggiano(), mew Garlic() , null)
}
}
9/7/2016 5
NYPizzaIngredientFactory (pg 147)
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
9/7/2016 6
CheesePizza (pg 150)
Instead of many
public class CheesePizza extends Pizza { ingredient parameters,
we just pass one.
PizzaIngredientFactory ingredientFactory;
void prepare() {
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce(); Creation
cheese = ingredientFactory.createCheese(); delegated
}
}
if (item.equals("cheese")) {
pizza = new CheesePizza(ingredientFactory);
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
}
return pizza;
9/7/2016 8
Abstract Factory Defined
9/7/2016 9
Factory Method vs. Abstract Factory
• Factory Method
– Uses inheritance to create a Concrete Product
– Sub classes decide which Concrete Product to use
• Abstract Factory
– Uses composition to create objects
– The objects created were a part of a family of
objects. For example, NY region had a specific set
of ingredients.
– An abstract factory actually contains one or more
Factory Methods!
9/7/2016 10
Database Example
• Imagine you wanted to created a database manager
object as an enterprise component. This object must
not be tied to a specific database implementation.
9/7/2016 13
A more interesting case…
• Class Explosion!
9/7/2016 15
Solution: Abstract Factory and
Prototype
When we create the abstract factory, pass the
different applicable products in the constructor.
PizzaIngredientFactory ingredientFactory =
new NYPizzaIngredientFactory
(new ThinCrustDough(), new MarinaraSauce(),
new Provolone() );
9/7/2016 16
Solution: Abstract Factory and
Prototype
• The abstract factory can then be passed to the different
types of Pizzas.
9/7/2016 17
Abstract Factory: Last Thoughts
9/7/2016 18
Summary
9/7/2016 19