Understanding Builder Pattern in Java and J2EE

Understanding Builder Pattern

Understanding Builder Pattern

In the last chapter, we discussed the Factory Pattern, which is one of the most commonly used design patterns in Java/J2EE. In this chapter, we are going to discuss the Builder Pattern which is used to build a complex object from the simple objects through a step by step approach. It falls under the category of creational design pattern as this pattern help us provide a common and very simple way to create a complex object from simple objects. In this pattern, the final object is built by a Builder class through a step by step approach that is independent of other objects.

Implementation of Builder Pattern
To understand the implementation of a builder pattern, lets take an example of a pizza restaurant that serves pizza and a drink as a lunch meal. Further a served pizza could be a veggie pizza or a cheese pizza packaged in a wrapper. Similarly, a served drink could be a mineral water or a soda drink packaged in a bottle or can. We have to create the following classes and interfaces in order to prepare a Builder Pattern for this pizza restaurant.


• A product interface which represents food products such as pizzas and drinks.
• A Wrapping interface which represents packaging of the food items
• The concrete classes which implement the product and Wrapping interfaces.
• The classes which implement the Wrapping interface as pizza would be packed in wrapped package and drink would be packed as a drink glass.
• A LunchMeal class that has an ArrayList of products.
• A LunchMealBuilder class to build different types of LunchMeal objects by combining products such as Veggie pizza with Mineral water, Veggie pizza with Soda drink, etc.
• A BuilderPatternDemonstration class which will use the LunchMealBuilder class to build a LunchMeal and generate the final bill along with the GST charges.

In the following example, we have demonstrated the above steps to explain the builder pattern in a very simple way.

Step 1: – Creation of an interface Product that represents the food and its wrapping.


Step 2: – Creation of the concrete classes which implement the Wrapping interface.



Step 3: – Creation of the abstract classes which implement the product interface providing the default functionalities.



Step 4: – Creation of the concrete classes extending Pizza and Drink classes.





Step 5: – Creation of a LunchMeal class which has these product objects.


Step 6: – Creation of a LunchMealBuilder class which is the actual builder class accountable for the creation of LunchMeal objects.


Step 7: – Creation of the BuiderPatternDemonstration class which uses LunchMealBuilder class to demonstrate the actual builder pattern.


Output: – When we execute the above BuiderPatternDemonstration class as a Java Application, we can observe the following output.

Source code for builder pattern in Java & J2EE

Conclusion: –
In this chapter, we discussed the builder pattern in detail along with suitable examples. In the example, we have demonstrated the building of a complex object from the simple objects through a step by step approach.


  1. Informative and Insightful blog!

    You have provided a great insight here about builder design pattern in java. Designer pattern allows to develop complicated object phase by phase and also makes sure a way to develop an object as a finished object. It is very helpful for all java developers to know about builder pattern in java, i have enjoyed this blog, thanks a lot for sharing!


Please enter your comment!
Please enter your name here