Builder Design Pattern

Builder design pattern

Hi Geeks, in this tutorial we will focus on the Builder Design Pattern. So let’s get started!

Concepts :

  • Handles complex constructors
  • Large number of parameters
  • Immutability

Examples of builders in the Java language: StringBuilder and the DocumentBuilder.

So, one of the problems solved by Builder is when you want to have different ways to construct an object and at the same time you want to make the object immutable (no one can change its values; no setters).

The intent of the Builder Pattern is to separate the complex construction of the object from its representation. So that the same construction process can be used to build different representations.

Let’s look into an example and how we can solve it using Builder.

Example : 

Let’s suppose that we want to create a sandiwch with the following ingredients: meat, mayonnaise , cheese and tomato.

The sandwich class :

The main class :

The SandwitchBuilder :

The main class  :

 

And That’s It for this tutorial. I hope you learned the pattern. The more you practice the more you get it .

                  Happy coding! 

 

 

 

You May Also Like

About the Author: Ibrahim belkhiria

Ibrahim is a Software engineer, Blogger, Youtuber and machine learning enthusiast .