CodesJava

Easy learning with example program codes

java builder design pattern


Java builder design pattern comes under the Creational Design Pattern category and it is used to build complex object from simple objects using step-by-step approach.

Main advantage of builder design pattern is that it provides better control over object construction process.

Let’s, consider the example of fast-food restaurant. A burger and a cold drink could be a meal type there. A burger can be Veg Burger or NonVeg Burger packed by a wrapper. Cold drink can be like Pespsi or Limca packed in a bottle.

Example

Item.java

package com.codesjava;
 
public interface Item {
   public String name();
   public float price();
   public Packing packing();
}

Packing.java

package com.codesjava;
 
public interface Packing {
	public String pack();
}

Wrapper.java

package com.codesjava;
 
public class Wrapper implements Packing {
   @Override
   public String pack() {
      return "Wrapper";
   }
}

Bottle.java

package com.codesjava;
 
public class Bottle implements Packing {
   @Override
   public String pack() {
      return "Bottle";
   }
}

Burger.java

package com.codesjava;
 
public abstract class Burger implements Item {
   @Override
   public Packing packing() {
      return new Wrapper();
   }
 
   @Override
   public abstract float price();
}

ColdDrink.java

package com.codesjava;
 
public abstract class ColdDrink implements Item {
   @Override
   public Packing packing() {
      return new Bottle();
   }
 
   @Override
   public abstract float price();
}

VegBurger.java

package com.codesjava;
 
public class VegBurger extends Burger {
   @Override
   public float price() {
      return 50.0f;
   }
 
   @Override
   public String name() {
      return "Veg Burger";
   }
}

NonVegBurger.java

package com.codesjava;
 
public class NonVegBurger extends Burger {
   @Override
   public float price() {
      return 100.0f;
   }
 
   @Override
   public String name() {
      return "NonVeg Burger";
   }
}

Pepsi.java

package com.codesjava;
 
public class Pepsi extends ColdDrink {
   @Override
   public float price() {
      return 35.0f;
   }
 
   @Override
   public String name() {
      return "Cold Drink Pepsi";
   }
}

Limca.java

package com.codesjava;
 
public class Limca extends ColdDrink {
   @Override
   public float price() {
      return 35.0f;
   }
 
   @Override
   public String name() {
      return "Cold Drink Limca";
   }
}

Meal.java

package com.codesjava;
 
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 displayItems(){	   
      for (Item item : items) {
         System.out.print("Item : " + item.name());
         System.out.print(", Packing : " + item.packing().pack());
         System.out.println(", Price : " + item.price());
      }		
   }
}

MealBuilder.java

package com.codesjava;
 
public class MealBuilder {
  public Meal buildVegMeal (){
      Meal meal = new Meal();
      meal.addItem(new VegBurger());
      meal.addItem(new Pepsi());
      return meal;
   }   
 
   public Meal buildNonVegMeal (){
      Meal meal = new Meal();
      meal.addItem(new NonVegBurger());
      meal.addItem(new Limca());
      return meal;
   }
}

Item.java

package com.codesjava;
 
public class BuilderPatternTest {
  public static void main(String args[]){
      MealBuilder mealBuilder = new MealBuilder();
 
      Meal vegMeal = mealBuilder.buildVegMeal();
      System.out.println("Veg Meal");
      vegMeal.displayItems();
      System.out.println("Total Cost: " + vegMeal.getCost());
 
      Meal nonVegMeal = mealBuilder.buildNonVegMeal();
      System.out.println("NonVeg Meal");
      nonVegMeal.displayItems();
      System.out.println("Total Cost: " + nonVegMeal.getCost());
  }
}

Output

Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 50.0
Item : Cold Drink Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.0
NonVeg Meal
Item : NonVeg Burger, Packing : Wrapper, Price : 100.0
Item : Cold Drink Limca, Packing : Bottle, Price : 35.0
Total Cost: 135.0
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki