8 minutes

Builder Design Pattern In Java

By Editorial @stacktips, On Sep 17, 2023 Design Pattern 2.75K Views

Over the course of this article, we will examine Builder design pattern in Java. Builder design pattern belongs to the creational family of pattern, that control the object instantiation process. Builder design pattern simplifies the object instantiation process for complex objects. The definition of Builder design pattern as provided in the original Gang of Four book is,

Separates the construction of a complex object from its representation so that the same construction process can create different representations.

Let us understand this pattern in a simpler term. In Java for instantiating objects we need to pass parameters to its class constructor. What if we have different combinations of parameters required to create objects and some of them are mandatory and some are optional? We can overload the class constructor with different combinations to achieve this.

Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

Builder Design Pattern Example

Let us take an example of a cafe preparing a meal. For preparing a meal there are various steps being performed, for example, choosing sandwich, adding sides, drink, offer, etc. There can be different combination of meal created during the process. In such cases preparing a meal is difficult than it is expected. Let us solve this issue by using builder design pattern.

Builder Design Pattern Java
In the above class diagram,

  1. The MealBuilder specifies an abstract interface for creating parts of a Meal.
  2. The SandwichMealBuilder is an concrete builder constructs and assembles parts of the product by implementing the MealBuilder interface.
  3. The MealDirector constructs an object using the MealBuilder interface.
  4. The Meal is the object which we are intended to create during this process

package com.javatechig.designpattern.builder;

public class Meal {
	public String sandwich;
	public String sideOrder;
	public String drink;
	public String offer;
	public double price;

	public String toString() {
		return "Sandwich=" + sandwich + " Side Order=" + sideOrder + " Drink="
				+ drink + " Offer=" + offer + " Price=" + price;

package com.javatechig.designpattern.builder;

public interface MealBuilder {	
	public abstract void addSandwich(String sandwich);
	public abstract void addSides(String sides);
	public abstract void addDrink(String drink);
	public abstract void addOffer(String coupon);
	public abstract void setPrice(double price);
	public abstract Meal getMeal();

package com.javatechig.designpattern.builder;

public class SandwichMealBuilder implements MealBuilder {
	private Meal _meal = new Meal();

	public void addSandwich(String sandwich) {
		_meal.sandwich = sandwich;

	public void addSides(String sides) {
		_meal.sideOrder = sides;

	public void addDrink(String drink) {
		_meal.drink = drink;

	public void addOffer(String coupon) {
		_meal.offer = coupon;

	public void setPrice(double price) {
		_meal.price = price;

	public Meal getMeal() {
		return _meal;

package com.javatechig.designpattern.builder;

public class MealDirector {
	public void makeMeal(MealBuilder mealBuilder) {
		mealBuilder.addOffer("Weekend Bonanza");

package com.javatechig.designpattern.builder;

public class Client {
	public static void main(String[] args) {		
		MealBuilder pizzaBuilder = new SandwichMealBuilder();
		MealDirector director = new MealDirector();
		Meal meal = pizzaBuilder.getMeal();
stacktips avtar


StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.