Section 6 - Part 1 (Builder Design Pattern)
Section 6 - Part 1 (Builder Design Pattern)
Engineering
Section (6) Part 1– Builder Design Pattern
1. Id
2. Name
3. Size
4. Crust
5. hasCheese
6. hasToopings
7. isSpicy
8. Price
9. dileveryTime
What are the problems with previous class?
• The constructor requires all fields to be passed, making it long and hard to read.
Pizza pizza = new Pizza(1, "Deluxe Feast", 14, "Stuffed Crust", true, true, true, 25.99, 30);
• This can lead to mistakes, such as passing parameters in the wrong order.
What are the problems with previous class?
• If some fields (e.g., hasExtraToppings or isSpicy) are optional, you must still include
them in the constructor.
• You might have to pass default values like false, null, or 0 for fields you don’t want,
which clutters the code.
Pizza pizza = new Pizza(1, null, 14, null, true, true, true, 25.99, 0);
• The class lacks flexibility in creating different representations of the Pizza object. For instance,
you may want to create:
• Simple Pizzas: Basic pizzas with only size, crust, and name.
• Deluxe Pizzas: Pizzas with extra toppings, cheese, and spices.
• Custom Pizzas: Pizzas with all fields, including delivery time and price.
• Handling these different representations becomes a challenge with the current design.
• You can attempt to solve this problem by creating multiple overloaded constructors, each
handling a specific variation of the Pizza object
• Steps:
• 1. Define the Product Class (Pizza)
• 2. Create the Builder Class
• 3. Add a Director Class (Optional)
• 4. Client Code
Solution (builder design pattern)
• 1. Define the Product Class (Pizza)
Solution (builder design pattern)
• 2. Create the Builder Class
Solution (builder design pattern)
• 3. Add a Director Class (Optional)
Solution (builder design pattern)
• 4. Client Code