DEV Community

Discussion on: Explain Design Pattern Builder Like I'm Five

Collapse
 
bijaykumarpun profile image
Bijay Kumar Pun

Since you said ELI5, here you go.
Imagine you are a wizard who builds a wide variety of potions using even a wider variety of ingredients.
One way you can make such potion is by using a class with as many constructor as the number of potion.
Eg: A constructor to make a potion with 3 ingredients will look like this:
MyIngredient(Ingredient1 ing, Ingredient2 ing2, Ingredient3 ing3);
Problem with it is you you need many constructors, and you can not swap places with ingredient; like what if you want to put ingredient2 first and then ingredient1.
This can be solved with Builder Pattern.
With builder pattern you can add as many ingredient as you want and in any possible combination.
So use builder pattern when you are building object with many properties, and the properties are not mandatory.

Collapse
 
axelledrouge profile image
AxelleDRouge • Edited

Thanks for the explanation, it's really clear.
And it confirms that this design correspond well to my situation.

my inner Harry Potter fan approves ;)
Snape Approves