I am trying to learn and implement Builder Pattern using a Pizza example.
I am trying to extend the solution I have to achieve 1 more thing:
Users should be able to order a specific Pizza ( Hawaiin, Meat, Veggie ) with a little bit of customization in it. Example --> In Meat Pizza --> Add Chicken + bacon
Looking for suggestions about implementing this in a cleaner way ( avoiding if-else )
Here is my Pizza and PizzaBuilder class:
public class Pizza {
private String dough;
private String sauce;
private String toppings;
public Pizza(PizzaBuilder pizzaBuilder) {
this.dough = pizzaBuilder.dough;
this.sauce = pizzaBuilder.sauce;
this.toppings = pizzaBuilder.toppings;
}
public void setDough(String dough) {
this.dough = dough;
}
public void setSauce(String sauce) {
this.sauce = sauce;
}
public void setToppings(String toppings) {
this.toppings = toppings;
}
abstract static class PizzaBuilder {
private String dough;
private String sauce;
private String toppings;
protected Pizza pizza;
abstract void addDough();
abstract void addSauce();
abstract void addToppings();
public PizzaBuilder addDough(String dough) {
this.dough = dough;
return this;
}
public PizzaBuilder addSauce(String sauce) {
this.sauce = sauce;
return this;
}
public PizzaBuilder addToppings(String toppings) {
this.toppings = toppings;
return this;
}
public Pizza bakeAPizza() {
return pizza;
}
public PizzaBuilder makeAPizza() {
getPizza();
this.addDough();
this.addSauce();
this.addToppings();
return this;
}
public PizzaBuilder getPizza() {
pizza = new Pizza(this);
return this;
}
}
@Override
public String toString() {
return "Pizza{" + "dough='" + dough + '\'' + ", sauce='" + sauce + '\'' + ", toppings='" + toppings + '\'' + '}';
}
}
Here is my MeatPizzaBuilder class:
public class MeatPizzaBuilder extends PizzaBuilder {
@Override
void addDough() {
pizza.setDough("Italian Wheat");
}
@Override
void addSauce() {
pizza.setSauce("mustard");
}
@Override
void addToppings() {
pizza.setToppings("Meat");
}
}
Similar to MeatPizzaBuilder: I have HawaainPizzaBuilder, VeggiePizzaBuilder and GenericPizzaBuilder. Here is my GenericPizzaBuilder class:
public class GenericPizzaBuilder extends PizzaBuilder {
@Override
void addDough() {
}
@Override
void addSauce() {
}
@Override
void addToppings() {
}
}
And Finally the PizzaShop ( main method )
public class PizzaShop {
public static void main(String[] args) {
Pizza hawaiinPizza = new HawaainPizzaBuilder().makeAPizza().bakeAPizza();
Pizza meatPizza = new MeatPizzaBuilder().makeAPizza().bakeAPizza();
Pizza veggiePizza = new VeggiePizzaBuilder().makeAPizza().bakeAPizza();
Pizza genericPizza = new GenericPizzaBuilder()
.addDough("Test")
.addSauce("Test Sauce")
.addToppings("All Veggeis minus Brocolli")
.getPizza()
.bakeAPizza();
System.out.println("Here is your Hawaiin Pizza: "+ hawaiinPizza.toString());
System.out.println("Here is your Meat Pizza: "+ meatPizza.toString());
System.out.println("Here is your Veggie Pizza: "+ veggiePizza.toString());
System.out.println("Here is your Generic Pizza: "+ genericPizza.toString());
}
}
So the User should be able to say :
I want a Meat Pizza with Chicken