image
Builder Design Pattern
Start course
Difficulty
Beginner
Duration
3h 59m
Students
273
Ratings
3.4/5
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

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. The 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 under 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 the argument. Some of the parameters might be optional, but in the factory pattern we are forced to send all the parameters and optional parameters needed 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 base 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. Concrete builder classes contain the functionality to create a particularly complex product.

There may be any number of concrete builder classes inheriting from builder. The director class controls the algorithm that generates the final product object. A director object is instantiated when its Construct() method is called. The method includes a parameter to capture the specific concrete builder object that is to be used to generate the product. The director then calls methods of the concrete builder in the correct order to generate the product object. On completion of the 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 Builder pattern. 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, name, packing type, and maybe 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 ColdDrink. Now, we have to define Coke types, CocaCola. Done, now we've finished. Let's define a Meal class which will need MenuItems. First define a list. Now, we will addItems. Of course, we 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 the structure and now we need builders, MealBuilder. addItems, add another. Done, now we can use this pattern. First create an instance from builder. Now, define a meal by using builder. Call show() method.

Let's add the other meal also. Done, let's try. As you can see, we have two options of menu lists with prices. So, I think that covers the builder design pattern. In the next video, we will look into the prototype pattern. So, I'll see you there. Bye, friends.

 

About the Author
Students
3969
Courses
64
Learning Paths
5

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.