From 0a7ef913dac2abb389b93b00c336e13e23002268 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 22 Dec 2020 08:41:47 +0000 Subject: [PATCH] Implement Factory Method Design Pattern --- Design Patterns/src/factory/Main.java | 35 ++++++++++ Design Patterns/src/factory/Pizza.java | 64 +++++++++++++++++++ Design Patterns/src/factory/PizzaStore.java | 18 ++++++ .../factory/chicago/ChicagoPizzaStore.java | 18 ++++++ .../chicago/ChicagoStyleCheesePizza.java | 19 ++++++ .../chicago/ChicagoStyleClamPizza.java | 20 ++++++ .../chicago/ChicagoStylePepperoniPizza.java | 23 +++++++ .../chicago/ChicagoStyleVeggiePizza.java | 22 +++++++ .../src/factory/ny/NYPizzaStore.java | 18 ++++++ .../src/factory/ny/NYStyleCheesePizza.java | 14 ++++ .../src/factory/ny/NYStyleClamPizza.java | 15 +++++ .../src/factory/ny/NYStylePepperoniPizza.java | 19 ++++++ .../src/factory/ny/NYStyleVeggiePizza.java | 18 ++++++ 13 files changed, 303 insertions(+) create mode 100644 Design Patterns/src/factory/Main.java create mode 100644 Design Patterns/src/factory/Pizza.java create mode 100644 Design Patterns/src/factory/PizzaStore.java create mode 100644 Design Patterns/src/factory/chicago/ChicagoPizzaStore.java create mode 100644 Design Patterns/src/factory/chicago/ChicagoStyleCheesePizza.java create mode 100644 Design Patterns/src/factory/chicago/ChicagoStyleClamPizza.java create mode 100644 Design Patterns/src/factory/chicago/ChicagoStylePepperoniPizza.java create mode 100644 Design Patterns/src/factory/chicago/ChicagoStyleVeggiePizza.java create mode 100644 Design Patterns/src/factory/ny/NYPizzaStore.java create mode 100644 Design Patterns/src/factory/ny/NYStyleCheesePizza.java create mode 100644 Design Patterns/src/factory/ny/NYStyleClamPizza.java create mode 100644 Design Patterns/src/factory/ny/NYStylePepperoniPizza.java create mode 100644 Design Patterns/src/factory/ny/NYStyleVeggiePizza.java diff --git a/Design Patterns/src/factory/Main.java b/Design Patterns/src/factory/Main.java new file mode 100644 index 0000000..e770d80 --- /dev/null +++ b/Design Patterns/src/factory/Main.java @@ -0,0 +1,35 @@ +package factory; + +import factory.chicago.ChicagoPizzaStore; +import factory.ny.NYPizzaStore; + +public class Main { + public static void main(String[] args) { + PizzaStore nyStore = new NYPizzaStore(); + PizzaStore chicagoStore = new ChicagoPizzaStore(); + + Pizza pizza = nyStore.orderPizza("cheese"); + System.out.println("Ethan ordered a " + pizza.getName() + "\n"); + + pizza = chicagoStore.orderPizza("cheese"); + System.out.println("Joel ordered a " + pizza.getName() + "\n"); + + pizza = nyStore.orderPizza("clam"); + System.out.println("Ethan ordered a " + pizza.getName() + "\n"); + + pizza = chicagoStore.orderPizza("clam"); + System.out.println("Joel ordered a " + pizza.getName() + "\n"); + + pizza = nyStore.orderPizza("pepperoni"); + System.out.println("Ethan ordered a " + pizza.getName() + "\n"); + + pizza = chicagoStore.orderPizza("pepperoni"); + System.out.println("Joel ordered a " + pizza.getName() + "\n"); + + pizza = nyStore.orderPizza("veggie"); + System.out.println("Ethan ordered a " + pizza.getName() + "\n"); + + pizza = chicagoStore.orderPizza("veggie"); + System.out.println("Joel ordered a " + pizza.getName() + "\n"); + } +} diff --git a/Design Patterns/src/factory/Pizza.java b/Design Patterns/src/factory/Pizza.java new file mode 100644 index 0000000..ea32cc1 --- /dev/null +++ b/Design Patterns/src/factory/Pizza.java @@ -0,0 +1,64 @@ +package factory; + +import java.util.ArrayList; +import java.util.List; + +public abstract class Pizza { + private String name; + private String dough; + private String sauce; + private final List toppings = new ArrayList<>(); + + public void prepare() { + System.out.println("Preparing " + name); + System.out.println("Tossing dough..."); + System.out.println("Adding sauce..."); + System.out.println("Adding toppings: "); + for (String topping : toppings) { + System.out.println(" " + topping); + } + } + + public void bake() { + System.out.println("Bake for 25 minutes at 350"); + } + + public void cut() { + System.out.println("Cutting the pizza into diagonal slices"); + } + + public void box() { System.out.println("Place pizza in official PizzaStore box"); } + + public String getName() { return name; } + + public void setName(String name) { + this.name = name; + } + + public void setDough(String dough) { + this.dough = dough; + } + + public void setSauce(String sauce) { + this.sauce = sauce; + } + + public List getToppings() { + return toppings; + } + + @Override + public String toString() { + StringBuilder display = new StringBuilder(); + display.append("---- ").append(name).append(" ----\n"); + display.append(dough).append("\n"); + display.append(sauce).append("\n"); + for (String topping : toppings) { + display.append(topping).append("\n"); + } + return display.toString(); + } +} + + + diff --git a/Design Patterns/src/factory/PizzaStore.java b/Design Patterns/src/factory/PizzaStore.java new file mode 100644 index 0000000..f656b80 --- /dev/null +++ b/Design Patterns/src/factory/PizzaStore.java @@ -0,0 +1,18 @@ +package factory; + +public abstract class PizzaStore { + + public abstract Pizza createPizza(String item); + + public Pizza orderPizza(String type) { + Pizza pizza = createPizza(type); + + System.out.println("--- Making a " + pizza.getName() + " ---"); + pizza.prepare(); + pizza.bake(); + pizza.cut(); + pizza.box(); + + return pizza; + } +} diff --git a/Design Patterns/src/factory/chicago/ChicagoPizzaStore.java b/Design Patterns/src/factory/chicago/ChicagoPizzaStore.java new file mode 100644 index 0000000..dc6016c --- /dev/null +++ b/Design Patterns/src/factory/chicago/ChicagoPizzaStore.java @@ -0,0 +1,18 @@ +package factory.chicago; + +import factory.Pizza; +import factory.PizzaStore; + +public class ChicagoPizzaStore extends PizzaStore { + + @Override + public Pizza createPizza(String item) { + return switch (item) { + case "cheese" -> new ChicagoStyleCheesePizza(); + case "veggie" -> new ChicagoStyleVeggiePizza(); + case "clam" -> new ChicagoStyleClamPizza(); + case "pepperoni" -> new ChicagoStylePepperoniPizza(); + default -> null; + }; + } +} diff --git a/Design Patterns/src/factory/chicago/ChicagoStyleCheesePizza.java b/Design Patterns/src/factory/chicago/ChicagoStyleCheesePizza.java new file mode 100644 index 0000000..84e3a45 --- /dev/null +++ b/Design Patterns/src/factory/chicago/ChicagoStyleCheesePizza.java @@ -0,0 +1,19 @@ +package factory.chicago; + +import factory.Pizza; + +public class ChicagoStyleCheesePizza extends Pizza { + + public ChicagoStyleCheesePizza() { + setName("Chicago Style Deep Dish Cheese Pizza"); + setDough("Extra Thick Crust Dough"); + setSauce("Plum Tomato Sauce"); + + getToppings().add("Shredded Mozzarella Cheese"); + } + + @Override + public void cut() { + System.out.println("Cutting the pizza into square slices"); + } +} diff --git a/Design Patterns/src/factory/chicago/ChicagoStyleClamPizza.java b/Design Patterns/src/factory/chicago/ChicagoStyleClamPizza.java new file mode 100644 index 0000000..c3c20c4 --- /dev/null +++ b/Design Patterns/src/factory/chicago/ChicagoStyleClamPizza.java @@ -0,0 +1,20 @@ +package factory.chicago; + +import factory.Pizza; + +public class ChicagoStyleClamPizza extends Pizza { + + public ChicagoStyleClamPizza() { + setName("Chicago Style Clam Pizza"); + setDough("Extra Thick Crust Dough"); + setSauce("Plum Tomato Sauce"); + + getToppings().add("Shredded Mozzarella Cheese"); + getToppings().add("Frozen Clams from Chesapeake Bay"); + } + + @Override + public void cut() { + System.out.println("Cutting the pizza into square slices"); + } +} diff --git a/Design Patterns/src/factory/chicago/ChicagoStylePepperoniPizza.java b/Design Patterns/src/factory/chicago/ChicagoStylePepperoniPizza.java new file mode 100644 index 0000000..13b322e --- /dev/null +++ b/Design Patterns/src/factory/chicago/ChicagoStylePepperoniPizza.java @@ -0,0 +1,23 @@ +package factory.chicago; + +import factory.Pizza; + +public class ChicagoStylePepperoniPizza extends Pizza { + + public ChicagoStylePepperoniPizza() { + setName("Chicago Style Pepperoni Pizza"); + setDough("Extra Thick Crust Dough"); + setSauce("Plum Tomato Sauce"); + + getToppings().add("Shredded Mozzarella Cheese"); + getToppings().add("Black Olives"); + getToppings().add("Spinach"); + getToppings().add("Eggplant"); + getToppings().add("Sliced Pepperoni"); + } + + @Override + public void cut() { + System.out.println("Cutting the pizza into square slices"); + } +} diff --git a/Design Patterns/src/factory/chicago/ChicagoStyleVeggiePizza.java b/Design Patterns/src/factory/chicago/ChicagoStyleVeggiePizza.java new file mode 100644 index 0000000..b20e72f --- /dev/null +++ b/Design Patterns/src/factory/chicago/ChicagoStyleVeggiePizza.java @@ -0,0 +1,22 @@ +package factory.chicago; + +import factory.Pizza; + +public class ChicagoStyleVeggiePizza extends Pizza { + + public ChicagoStyleVeggiePizza() { + setName("Chicago Deep Dish Veggie Pizza"); + setDough("Extra Thick Crust Dough"); + setSauce("Plum Tomato Sauce"); + + getToppings().add("Shredded Mozzarella Cheese"); + getToppings().add("Black Olives"); + getToppings().add("Spinach"); + getToppings().add("Eggplant"); + } + + @Override + public void cut() { + System.out.println("Cutting the pizza into square slices"); + } +} diff --git a/Design Patterns/src/factory/ny/NYPizzaStore.java b/Design Patterns/src/factory/ny/NYPizzaStore.java new file mode 100644 index 0000000..6f869e8 --- /dev/null +++ b/Design Patterns/src/factory/ny/NYPizzaStore.java @@ -0,0 +1,18 @@ +package factory.ny; + +import factory.Pizza; +import factory.PizzaStore; + +public class NYPizzaStore extends PizzaStore { + + @Override + public Pizza createPizza(String item) { + return switch (item) { + case "cheese" -> new NYStyleCheesePizza(); + case "veggie" -> new NYStyleVeggiePizza(); + case "clam" -> new NYStyleClamPizza(); + case "pepperoni" -> new NYStylePepperoniPizza(); + default -> null; + }; + } +} diff --git a/Design Patterns/src/factory/ny/NYStyleCheesePizza.java b/Design Patterns/src/factory/ny/NYStyleCheesePizza.java new file mode 100644 index 0000000..1769402 --- /dev/null +++ b/Design Patterns/src/factory/ny/NYStyleCheesePizza.java @@ -0,0 +1,14 @@ +package factory.ny; + +import factory.Pizza; + +public class NYStyleCheesePizza extends Pizza { + + public NYStyleCheesePizza() { + setName("NY Style Sauce and Cheese Pizza"); + setDough("Thin Crust Dough"); + setSauce("Marinara Sauce"); + + getToppings().add("Grated Reggiano Cheese"); + } +} diff --git a/Design Patterns/src/factory/ny/NYStyleClamPizza.java b/Design Patterns/src/factory/ny/NYStyleClamPizza.java new file mode 100644 index 0000000..633b585 --- /dev/null +++ b/Design Patterns/src/factory/ny/NYStyleClamPizza.java @@ -0,0 +1,15 @@ +package factory.ny; + +import factory.Pizza; + +public class NYStyleClamPizza extends Pizza { + + public NYStyleClamPizza() { + setName("NY Style Clam Pizza"); + setDough("Thin Crust Dough"); + setSauce("Marinara Sauce"); + + getToppings().add("Grated Reggiano Cheese"); + getToppings().add("Fresh Clams from Long Island Sound"); + } +} diff --git a/Design Patterns/src/factory/ny/NYStylePepperoniPizza.java b/Design Patterns/src/factory/ny/NYStylePepperoniPizza.java new file mode 100644 index 0000000..2a6c7a5 --- /dev/null +++ b/Design Patterns/src/factory/ny/NYStylePepperoniPizza.java @@ -0,0 +1,19 @@ +package factory.ny; + +import factory.Pizza; + +public class NYStylePepperoniPizza extends Pizza { + + public NYStylePepperoniPizza() { + setName("NY Style Pepperoni Pizza"); + setDough("Thin Crust Dough"); + setSauce("Marinara Sauce"); + + getToppings().add("Grated Reggiano Cheese"); + getToppings().add("Sliced Pepperoni"); + getToppings().add("Garlic"); + getToppings().add("Onion"); + getToppings().add("Mushrooms"); + getToppings().add("Red Pepper"); + } +} diff --git a/Design Patterns/src/factory/ny/NYStyleVeggiePizza.java b/Design Patterns/src/factory/ny/NYStyleVeggiePizza.java new file mode 100644 index 0000000..602bddc --- /dev/null +++ b/Design Patterns/src/factory/ny/NYStyleVeggiePizza.java @@ -0,0 +1,18 @@ +package factory.ny; + +import factory.Pizza; + +public class NYStyleVeggiePizza extends Pizza { + + public NYStyleVeggiePizza() { + setName("NY Style Veggie Pizza"); + setDough("Thin Crust Dough"); + setSauce("Marinara Sauce"); + + getToppings().add("Grated Reggiano Cheese"); + getToppings().add("Garlic"); + getToppings().add("Onion"); + getToppings().add("Mushrooms"); + getToppings().add("Red Pepper"); + } +}