Implement Factory Method Design Pattern

master
Hammy 4 years ago
parent bd1b04c877
commit 0a7ef913da

@ -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");
}
}

@ -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<String> 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<String> 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();
}
}

@ -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;
}
}

@ -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;
};
}
}

@ -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");
}
}

@ -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");
}
}

@ -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");
}
}

@ -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");
}
}

@ -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;
};
}
}

@ -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");
}
}

@ -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");
}
}

@ -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");
}
}

@ -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");
}
}
Loading…
Cancel
Save