Tutorials Design Patterns Builder Design Pattern In Java

Builder Design Pattern In Java

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

Meal.java

package com.javatechig.designpattern.builder;

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

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

MealBuilder.java

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

SandwichMealBuilder.java

package com.javatechig.designpattern.builder;

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

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

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

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

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

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

	@Override
	public Meal getMeal() {
		return _meal;
	}
}

MealDirector.java

package com.javatechig.designpattern.builder;

public class MealDirector {
	
	public void makeMeal(MealBuilder mealBuilder) {
		
		mealBuilder.addSandwich("Hamburger");
		mealBuilder.addSides("Fries");
		mealBuilder.addDrink("Coke");
		mealBuilder.addOffer("Weekend Bonanza");
		mealBuilder.setPrice(5.99);
	}
}

Client.java

package com.javatechig.designpattern.builder;

public class Client {
	public static void main(String[] args) {		
		MealBuilder pizzaBuilder = new SandwichMealBuilder();
		MealDirector director = new MealDirector();
		director.makeMeal(pizzaBuilder);
		
		Meal meal = pizzaBuilder.getMeal();
		System.out.println(meal.toString());
	}
}
Total
0
Shares
Leave a Reply

Your email address will not be published.

Previous Post
Java Design Pattern Tutorials

Prototype Design Pattern In Java

Next Post
Factory Method Design Pattern

Factory Method Design Pattern in Java

Related Posts

State Design Pattern in Java

Over the course of this article, we will examine State design pattern in java with help of realtime examples. The State design pattern belongs to the behavioral family of pattern that deals with the runtime object behavior based on the current state. The definition of State Design Pattern as per the original Gang of Four book is; "Allows an object to alter its behavior when its internal state changes. The object will appear to change its class".
Read more
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All