The Builder Pattern uses multiple simple objects to build a complex object step by step. This type of design pattern is a creational pattern, which provides an optimal way to create objects.
Advantages: 1. The builder is independent and easy to expand. 2. It is easy to control the risk of details.
Disadvantages: 1. The products must have something in common, and the scope is limited. 2. If the internal changes are complicated, there will be many construction types.
Usage scenarios: 1. The objects to be generated have complex internal structures. 2. The internal properties of the objects that need to be generated are mutually dependent on each other.
Note: The difference from the factory mode is that the builder mode is more concerned with the order of assembly of parts.
Let's assume a business case for a fast food restaurant, where a typical meal can be a burger and a cold drink. Burgers can be either Veg Burger or Chicken Burger, and they come in a carton. Cold drinks can be Coca-Cola (coke) or Pepsi (pepsi), they are bottled.
//create an object class
public interface Item {
public String name(); public Packing packing(); public float price(); } //Create a wrapper class public interface Packing { public String pack(); } //The wrapper class implements the wrapper interface public class Wrapper implements Packing { @Override public String pack() { return "Wrapper"; }} //The bottle class implements the packaging interface public class Bottle implements Packing { @Override public String pack() { return "Bottle"; }} //Create an abstract class to implement the entity class (because the concrete entity class does not know) public abstract class Burger implements Item { @Override public Packing packing() { return new Wrapper(); } @Override public abstract float price();} //Create a cold drink entity class public abstract class ColdDrink implements Item { @Override public Packing packing() { return new Bottle(); } @Override public abstract float price();} //Create a veggie burger inheriting from the hamburger class public class VegBurger extends Burger { @Override public float price() { return 25.0f; } @Override public String name() { return "Veg Burger"; }} //Create a chicken burger to implement the burger class public class ChickenBurger extends Burger { @Override public float price() { return 50.5f; } @Override public String name() { return "Chicken Burger"; }} //Create a Coca-Cola implementation of the cold drink class public class Coke extends ColdDrink { @Override public float price() { return 30.0f; } @Override public String name() { return "Coke"; } //Create a Pepsi to implement the cold drink class public class Pepsi extends ColdDrink { @Override public float price() { return 35.0f; } @Override public String name() { return "Pepsi"; }} //Food class (drinks and hamburgers) import java.util.ArrayList; import java.util.List; public class Meal { private List<Item> items = new ArrayList<Item>(); public void addItem(Item item){ items.add(item); } public float getCost(){ float cost = 0.0f; for (Item item : items) { cost += item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.print("Item : "+item.name()); System.out.print(", Packing : "+item.packing().pack()); System.out.println(", Price : "+item.price()); } } } //create food public class MealBuilder { public Meal prepareVegMeal (){ Meal meal = new Meal(); meal.addItem(new VegBurger()); meal.addItem(new Coke()); return meal; } public Meal prepareNonVegMeal (){ Meal meal = new Meal(); meal.addItem(new ChickenBurger()); meal.addItem(new Pepsi()); return meal; }} //accomplish public class BuilderPatternDemo { public static void main(String[] args) { MealBuilder mealBuilder = new MealBuilder(); Meal vegMeal = mealBuilder.prepareVegMeal(); System.out.println("Veg Meal"); vegMeal.showItems(); System.out.println("Total Cost: " +vegMeal.getCost()); Meal nonVegMeal = mealBuilder.prepareNonVegMeal(); System.out.println("\n\nNon-Veg Meal"); nonVegMeal.showItems(); System.out.println("Total Cost: " +nonVegMeal.getCost()); }}