brittany johnson csci360 abstract factory design pattern
Post on 19-Dec-2015
221 Views
Preview:
TRANSCRIPT
Name: Abstract Factory
Nickname: Kit or Toolkit
What is it? A Creational software design pattern; similar to the Factory Method pattern.
What does it do? It provides a way of encapsulating individual factories into a group with a common theme.
Background
Benefits: low coupling ease of changeability promotes consistency
Downfall: difficult to support new kinds of products
Consequences
public interface PizzaIngredientFactory {
Dough createDough();
Sauce createSauce();
Cheese createCheese();
}
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
public NYPizzaIngredientFactory(){
}
public Cheese createCheese(){
return new ReggianoCheese();
}
public Dough createDough(){
return new ThinCrustDough();
}
public Sauce createSauce(){
return new MarinaraSauce();
}
public Veggies [] createVeggies(){
Veggies veggies [] = {new Olives(),
new GreenPeppers() };
return veggies;
}
public Pepperoni createPepperoni(){
return new ThinPepperoni();
}
}
Abstract Factory & Factory
public class NYPizza extends Pizza{
public NYPizza(){
}
public void prepare(String PizzaType){
NYPizzaIngredientFactory NYP = new NYPizzaIngredientFactory();
NYP.createCheese();
NYP.createDough();
NYP.createPepperoni();
NYP.createSauce();
NYP.createVeggies();
}
}
public static void main(String[] args) {
System.out.println("What kind of Pizza would you like? Press NY for New York style and C for Chicago style.");
String pType = "";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
pType = in.readLine();
if (pType.equals("NY")){
NYPizza nyPizza = new NYPizza();
nyPizza.prepare(pType);
System.out.println("Your New York Style Pizza has been created.");
}
......
}
Subclass and Client
top related