Rize S. answered 03/23/23
Senior IT Certified Trainer, IT Developer & DBA Administrator
Design Solution:
We can use the Decorator design pattern to implement the pizza ordering system. The decorator pattern allows us to dynamically add additional features to an object at runtime. We can start with a basic pizza class, and then add decorators for the different toppings.
First, we can define an interface for our pizza class, which will have a method to get the description and another method to get the cost.
public interface Pizza {
String getDescription();
double getCost();
}
Next, we can create two concrete pizza classes: ThinCrust and Regular, which will implement the Pizza interface.
public class ThinCrust implements Pizza {
@Override
public String getDescription() {
return "Thin Crust Pizza";
}
@Override
public double getCost() {
return 100.0;
}
}
public class Regular implements Pizza {
@Override
public String getDescription() {
return "Regular Pizza";
}
@Override
public double getCost() {
return 80.0;
}
}
Now, we can create decorators for the different toppings: Cheese, Olive, and ChickenChunks.
public abstract class PizzaDecorator implements Pizza {
protected Pizza pizza;
public PizzaDecorator(Pizza pizza) {
this.pizza = pizza;
}
@Override
public String getDescription() {
return pizza.getDescription();
}
@Override
public double getCost() {
return pizza.getCost();
}
}
public class Cheese extends PizzaDecorator {
public Cheese(Pizza pizza) {
super(pizza);
}
@Override
public String getDescription() {
return pizza.getDescription() + ", Cheese";
}
@Override
public double getCost() {
return pizza.getCost() + 70.0;
}
}
public class Olive extends PizzaDecorator {
public Olive(Pizza pizza) {
super(pizza);
}
@Override
public String getDescription() {
return pizza.getDescription() + ", Olive";
}
@Override
public double getCost() {
return pizza.getCost() + 55.0;
}
}
public class ChickenChunks extends PizzaDecorator {
public ChickenChunks(Pizza pizza) {
super(pizza);
}
@Override
public String getDescription() {
return pizza.getDescription() + ", Chicken Chunks";
}
@Override
public double getCost() {
return pizza.getCost() + 65.0;
}
}
Finally, we can create a main class to test the pizza ordering system.
public class PizzaStore {
public static void main(String[] args) {
Pizza thinCrust = new ThinCrust();
Pizza regular = new Regular();
Pizza regularWithCheese = new Cheese(regular);
Pizza regularWithOliveAndChickenChunks = new Olive(new ChickenChunks(regular));
System.out.println(thinCrust.getDescription() + " - " + thinCrust.getCost());
System.out.println(regular.getDescription() + " - " + regular.getCost());
System.out.println(regularWithCheese.getDescription() + " - " + regularWithCheese.getCost());
System.out.println(regularWithOliveAndChickenChunks.getDescription() + " - " + regularWithOliveAndChickenChunks.getCost());
}
}
Output:
Thin Crust Pizza - 100.0
Regular Pizza - 80.0
Regular Pizza, Cheese - 150.0
Regular Pizza, Olive, Chicken Chunks - 200.0
The program allows for easy addition of new pizza types and toppings by simply creating new concrete classes and decorators.