Design Patterns Builder Pattern - Design Patterns

What is Design Pattern Builder Pattern?

Builder pattern builds a simple object using difficult substance and using a step by step move towards. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to make an object.

A Builder class builds the ultimate object step by step. This builder is dependent of other substance.

Implementation

We have careful a business case of fast-food café where a typical meal could be a burger and a cold drink. Burger could be each a Vega Burger or Chicken Burger and will be packed by a packaging. Cold drink could be each a coke or Pepsi and will be packed in a bottle.

We are going to generate an Item interface representative food items such as burgers and cold drinks and real classes implementing the Item interface and a Packing interface representing packaging of food items and concrete classes implementing the Packing interface as burger would be packed in wrapper and cold drink would be packed as bottle.

We then construct a Meal class having Array List of Item and a Meal Builder to build altered types of Meal objects by combining Item. BuilderPatternDemo, our demo class will use Meal Builder to build a Meal.
builder_pattern_uml_diagram

Step 1

Create an interface Item representing food item and packing.

Item.java

Packing.java

Step 2

Create concrete classes implementing the Packing interface.

Wrapper.java

Bottle.java

Step 3

Create abstract classes implementing the item interface provide default functionalities.

Burger.java

ColdDrink.java

Step 4

Create concrete classes extending Burger and Cold Drink classes.

VegBurger.java

ChickenBurger.java

Coke.java

Pepsi.java

Step 5

Create a Meal class having Item objects defined above.

Meal.java

Step 6

Create a Meal Builder class, the concrete builder class responsible to generate Meal objects.

MealBuilder.java

Step 7

BuiderPatternDemo uses MealBuider to display builder pattern.

BuilderPatternDemo.java

Step 8

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics