Creational Design Pattern
![](https://codelido.com/assets/files/2022-12-17/1671311203-257768-single-ton-design-pattern-22.jpg)
Definition
The builder pattern was introduced to solve some of the problems with factory and abstract Factory design patterns when the object contains a lot of attributes. This 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.
Characteristics
- The Builder Pattern is a creational design pattern used to construct complex objects step by step.
- It provides a flexible way to create objects and allows for the construction of complex objects with multiple variables.
- The builder pattern provides a separate builder class that encapsulates the construction logic and is used to create the final object.
- The builder class contains all the necessary methods to create the object and all the necessary parameters to construct it.
- It allows the client code to be decoupled from the complex object creation process.
- The pattern is useful when the construction process of an object is complex and involves multiple steps.
UML Diagram
![](https://codelido.com/assets/files/2022-12-17/1671309345-285917-image.png)
Code
public interface Item {
public String name();
public Packing packing();
public float price();
}
public interface Packing {
public String pack();
}
public class Wrapper implements Packing {
@Override
public String pack() {
return "Wrapper";
}
}
public class Bottle implements Packing {
@Override
public String pack() {
return "Bottle";
}
}
public abstract class Burger implements Item {
@Override
public Packing packing() {
return new Wrapper();
}
@Override
public abstract float price();
}
public abstract class ColdDrink implements Item {
@Override
public Packing packing() {
return new Bottle();
}
@Override
public abstract float price();
}
public class VegBurger extends Burger {
@Override
public float price() {
return 25.0f;
}
@Override
public String name() {
return "Veg Burger";
}
}
public class ChickenBurger extends Burger {
@Override
public float price() {
return 50.5f;
}
@Override
public String name() {
return "Chicken Burger";
}
}
public class Coke extends ColdDrink {
@Override
public float price() {
return 30.0f;
}
@Override
public String name() {
return "Coke";
}
}
public class Pepsi extends ColdDrink {
@Override
public float price() {
return 35.0f;
}
@Override
public String name() {
return "Pepsi";
}
}
import java.util.ArrayList;
import java.util.List;
public class Meal {
private List<Item> items = new ArrayList<Item>();
public void addItem(Item item){
items.add(item);
}
public float getCost(){
float cost = 0.0f;
for (Item item : items) {
cost += item.price();
}
return cost;
}
public void showItems(){
for (Item item : items) {
System.out.print("Item : " + item.name());
System.out.print(", Packing : " + item.packing().pack());
System.out.println(", Price : " + item.price());
}
}
}
public class MealBuilder {
public Meal prepareVegMeal (){
Meal meal = new Meal();
meal.addItem(new VegBurger());
meal.addItem(new Coke());
return meal;
}
public Meal prepareNonVegMeal (){
Meal meal = new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi());
return meal;
}
}
public class BuilderPatternDemo {
public static void main(String[] args) {
MealBuilder mealBuilder = new MealBuilder();
Meal vegMeal = mealBuilder.prepareVegMeal();
System.out.println("Veg Meal");
vegMeal.showItems();
System.out.println("Total Cost: " + vegMeal.getCost());
Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
System.out.println("Total Cost: " + nonVegMeal.getCost());
}
}
Output
Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0
Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5
Advantages
- It is more efficient than creating objects directly with the new operator because it can take advantage of optimization techniques such as caching and lazy initialization.
- It is easier to read and maintain because the code is more organized and structured.
- It allows for better control over object configuration by providing the ability to define parameters and properties.
- It is easy to customize and extend the builder pattern.
- It is useful when dealing with complex objects that require multiple steps to construct.
Disadvantages
- Over-engineering: Builder pattern can cause unnecessary complexity when dealing with simple objects. For example, a builder pattern might be overkill for an object with only a few fields.
- Costly: The Builder pattern requires creating multiple objects, which can be costly if the object is simple and used frequently.
- Harder to read and maintain: The Builder pattern can make code harder to read, as the code is spread out over multiple classes. This makes it more difficult to maintain.
- Limited to constructing one object: The Builder pattern is not suitable for scenarios where you need to construct multiple related objects.