# Builder

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Builder (which is a Creational Pattern).

# Builder - theory

The Builder pattern, is a way to construct complex objects step by step using a builder object. This design pattern provide a fine control over the construction process and provides clear separation between the construction and representation of an object. A Builder class builds the final object step by step.

Here it is the UML Diagram for Builder Pattern Example:

# Builder - example

Here are the classes for showing how the Prototype Java Design Pattern works:

Item.java
public interface Item {
   public String getName();
   public float getPrice();
}
BigMac.java
public class BigMac implements Item{
 
    public String getName(){
        return "Big Mac";
    };
    
    public float getPrice(){
        return (float) 2.3;
    }
}
IceCream.java
public class IceCream implements Item{
 
    public String getName(){
        return "Ice Cream";
    };
    
    public float getPrice(){
        return (float) 0.9;
    }
}
CocaCola.java
public class CocaCola implements Item{
 
    public String getName(){
        return "Coca Cola";
    };
    
    public float getPrice(){
        return (float) 1.2;
    }
}
Tea.java
public class Tea implements Item{
 
    public String getName(){
        return "Tea";
    };
    
    public float getPrice(){
        return (float)0.5;
    }
}
Meal.java
import java.util.ArrayList;
import java.util.List;
 
public class Meal {
    private List<Item> items = new ArrayList<Item>();
     
    public void getItems(){
         for (Item item : items) {
                 System.out.println("Item : " + item.getName() + ", Price : " + item.getPrice());
          }     
    }
        
    public void addItem(Item item){
          items.add(item);
    }
 
    public float getCost(){
       float cost = 0.0f;
           
       for (Item item : items) {
             cost += item.getPrice();
       }        
       return cost;
    }
}
MealBuilder.java
public class MealBuilder {
     
    public Meal createMenu1 (){
         Meal meal = new Meal();
         meal.addItem(new BigMac());
         meal.addItem(new CocaCola());
         meal.addItem(new IceCream());
     
         return meal;
    }  
     
    public Meal createMenu2 (){
         Meal meal = new Meal();
         meal.addItem(new BigMac());
         meal.addItem(new CocaCola());
 
         return meal;
    } 
     
    public Meal sellTea(){
         Meal meal = new Meal();
         meal.addItem(new Tea());
 
         return meal;
    } 
}
BuilderPatternExample.java
public class BuilderPatternExample {
     
    public static void main(String[] args) {
        
        MealBuilder mealBuilder = new MealBuilder();
 
        Meal menu1 = mealBuilder.createMenu1();
        System.out.println("Menu1 was buit:");
        System.out.println("-----------------------");
        menu1.getItems();
        System.out.println("This meal costs: " + menu1.getCost());
 
        Meal menu2 = mealBuilder.createMenu2();
        System.out.println("");
        System.out.println("Menu2 was buit:");
        System.out.println("-----------------------");
        menu2.getItems();
        System.out.println("This meal costs: " + menu2.getCost());
         
        Meal tea = mealBuilder.sellTea();
        System.out.println("");
        System.out.println("One tea was sold:");
        System.out.println("-----------------------");
        tea.getItems();
        System.out.println("This meal costs: " + tea.getCost());
         
 }
}

And here you have the example result :