Builder Pattern
Start course
4h 56m

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.



Hello dear friends. In this video, we will examine the Builder Design Pattern on Java, let's begin. The builder pattern is a design pattern that allows for the step by step creation of complex objects using the correct sequence of actions. This construction is controlled by a director object that only needs to know the type of object it is to create. This type of design pattern comes from creational pattern as this pattern provides one of the best ways to create an object. Builder pattern aims to separate the construction of a complex object from its representation so that the same construction process can create different representations. It's used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object. There are three significant issues with factory and abstract factory design patterns when the object contains a lot of attributes.

Too many arguments to pass from the client program to the factory class can be error prone because most of the time the type of arguments are the same and from the client side, it's hard to maintain the order of argument. Some of the parameters might be optional, but in the factory pattern, we are forced to send all the parameters and optional parameters need to send as null. If the object is heavy and its creation is complex, then all that complexity will be part of factory classes, that is confusing. We can solve the issues with many parameters by providing a constructor with required parameters and then different setter methods to set the optional parameters. The problem with this approach is that the object state will be inconsistent unless all the attributes are set explicitly. Builder pattern solves the issue with a large number of optional parameters and inconsistent state by providing a way to build the object step by step and provide a method that will actually return the final object. You can see the structure of the builder pattern in the illustrated UML diagram.

The product class defines the type of the complex object that is to be generated by the builder pattern. Builder is an abstract-based class that defines all of the steps that must be taken in order to correctly create a product. Each step is generally abstract as the actual functionality of the builder is carried out in the concrete subclasses. The getProduct method is used to return the final product. The Builder class is often replaced with a simple interface. ConcreteBuilder classes contain the functionality to create a particularly complex product. There may be any number of ConcreteBuilder classes inheriting from builder. The Director class controls the algorithm that generates the final product object. A Director object is instantiated when it's construct method is called. The method includes a parameter to capture the specific ConcreteBuilder object that is to be used to generate the product.

The Director then calls methods of the ConcreteBuilder in the correct order to generate the product object. On completion of process, the getProduct method of the builder object can be used to return the product. Let's now apply the builder design pattern to a Java project. Create a new project and name it BuilderPattern. If you prefer to keep all design patterns together, you can work on one of your older projects. I want to implement this pattern to menu items. First, we need an interface, we'll call this MenuItem. We have to determine the properties that will be part of this interface, name and maybe packing type, price. For packing, we need another interface, String pack, let's define packing type classes. Wrapper implements packing. Add another class and name it bottle.

Now it's time to define the menu items. First one is Burger, implements item. We have to override methods, packing, price. Let's add a drink, ColdDrink. Override methods. Let's create Concrete classes extending Burger and ColdDrink classes, VegBurger. Give price, add another. Name will be ChickenBurger. Let's define a Coke from cold drink. Now we have to define Coke types. CocaCola. Done. Now we have finished. Let's define a meal class which will need menu items. First, define a list. Now we will add items. Of course, we will need to add getCost method for payments. If you want, let's add a new method to show all of them. Done, we have finished structure, now we need builders, MealBuilder, create constructor, add items, add another. Done. Now we can use this pattern. First, create an instance from builder. Now define Meal by using Builder. Call Show method. Let's add another meal also. Done, let's try. As you can see, we have two options of menu lists with prices. I think that covers the Builder Design Pattern. In the following video, we will look into the Prototype Pattern. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics