Description

The Builder design pattern is one of the Creational Design Patterns, according to which a complex object can be generated using simple objects.

  • It provides one of the best ways to create an object.
  • It is used when a complex object is to be created using simple objects.

Business Case

Let's consider a business case of a fast-food restaurant that has the below structure.

  • Contains a meal that could include a burger and a drink.
  • The burger can be a veg or non-veg burger, packed in a wrapper.
  • The drink can be a Coke or Pepsi, packed in a bottle.

In order to achieve the above structure, we may define classes as below.

  • Interface "Item" represent food items, like a burger, cool drink, etc.,
  • Interface "Packing" represents food packaging, like a wrapper, bottle, etc.,
  • Concrete classes to implement the "Item" interface.
  • Concrete classes to implement the "Packing" interface.
  • Finally, create a "Meal" class having ArrayList of "Item" and a "MealBuilder" to build different types of "Meal" objects by combining "Item" objects.

Implementation in Java

Let's follow the below approach to implement this design pattern in Java.

  • Create interfaces "Item" and "Packing".
  • Create concrete classes to implement "Item" and "Packing" interfaces separately.
  • Create abstract classes implementing the item interface providing default functionalities.
  • Create concrete classes extending Burger and ColdDrink classes.
  • Create a Meal class having Item objects defined above.
  • Create a MealBuilder class, the actual builder class responsible to create Meal objects.
  • Finally, the BuiderPatternDemo uses MealBuider to demonstrate the builder pattern.

Step 1: Create interfaces "Item" and "Packing"

Item.java

public interface Item {
   public String name();
   public Packing packing();
   public float price();	
}

Packing.java

public interface Packing {
   public String pack();
}

Step 2: Create concrete classes implementing the Packing interface

Wrapper.java

public class Wrapper implements Packing {
   @Override
   public String pack() {
      return "Wrapper";
   }
}

Bottle.java

public class Bottle implements Packing {
   @Override
   public String pack() {
      return "Bottle";
   }
}

Step 3: Create abstract classes implementing the item interface providing default functionalities

Burger.java

public abstract class Burger implements Item {

   @Override
   public Packing packing() {
      return new Wrapper();
   }

   @Override
   public abstract float price();
}

CoolDrink.java

public abstract class ColdDrink implements Item {

	@Override
	public Packing packing() {
       return new Bottle();
	}

	@Override
	public abstract float price();
}

Step 4: Create concrete classes extending Burger and ColdDrink classes

VegBurger.java

public class VegBurger extends Burger {

   @Override
   public float price() {
      return 25.0f;
   }

   @Override
   public String name() {
      return "Veg Burger";
   }
}

ChickenBurger.java

public class ChickenBurger extends Burger {

   @Override
   public float price() {
      return 50.5f;
   }

   @Override
   public String name() {
      return "Chicken Burger";
   }
}

Coke.java

public class Coke extends ColdDrink {

   @Override
   public float price() {
      return 30.0f;
   }

   @Override
   public String name() {
      return "Coke";
   }
}

Pepsi.java

public class Pepsi extends ColdDrink {

   @Override
   public float price() {
      return 35.0f;
   }

   @Override
   public String name() {
      return "Pepsi";
   }
}

Step 5: Create a Meal class having Item objects defined above.

Meal.java

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

Step 6: Create a MealBuilder class, the actual builder class responsible to create Meal objects.

MealBuilder.java

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

Step 7: BuiderPatternDemo uses MealBuider to demonstrate the builder pattern.

BuilderPatternDemo.java

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

Step 8: Execute the code to verify the output.

Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0


Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5

Overall

We now know about the Builder design pattern and its implementation.

Related Links