Download presentation
Presentation is loading. Please wait.
1
Brittany Johnson CSCI360 Abstract Factory Design Pattern
2
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
3
Benefits: low coupling ease of changeability promotes consistency Downfall: difficult to support new kinds of products Consequences
4
UML Diagram
5
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
6
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
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.