Reference Website
Below tutorial has very clearly explained the implementation of Builder Design Pattern
http://ramj2ee.blogspot.in/2013/12/builder-design-pattern-implementation.html#.WB4gMGVeDR0
package coffee_builder;
import java.util.*;
public class customer {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc1 = new Scanner(System.in);
System.out.println("Please enter Drink Name");
String input = sc1.next();
Director.takeOrder(input);
}
}
package coffee_builder;
//Also called as Waiter
public class Director {
public static Beverage takeOrder(String beverageType)
{
BeverageBuilder bb = null;
if(beverageType.equalsIgnoreCase("Tea"))
{
bb = new TeaBuilder();
}
else if(beverageType.equalsIgnoreCase("Coffee"))
{
bb = new CoffeeBuilder();
}
else
{
System.out.println("Sorry we cannot take order other than Coffee and Tea");
}
return bb.buildBeverage();
}
}
package coffee_builder;
public class TeaBuilder extends BeverageBuilder{
@Override
void setBeverageType() {
// TODO Auto-generated method stub
System.out.println("Preparing Tea");
getBeverage().setBeverageName("Tea");
}
@Override
void setWater() {
// TODO Auto-generated method stub
System.out.println("Adding 15 ml of water");
getBeverage().setWater(15);
}
@Override
void setMilk() {
// TODO Auto-generated method stub
System.out.println("Adding 30 ml of milk");
getBeverage().setMilk(30);
}
@Override
void setSugar() {
// TODO Auto-generated method stub
System.out.println("Adding 50 ml of sugar");
getBeverage().setSugar(50);
}
@Override
void setPowderQuantity() {
// TODO Auto-generated method stub
System.out.println("Adding 3 table spoons of tea Powder");
getBeverage().setPowderQuantity(3);
}
@Override
Beverage createBeverage() {
// TODO Auto-generated method stub
return new Beverage();
}
}
package coffee_builder;
public class CoffeeBuilder extends BeverageBuilder{
@Override
void setBeverageType() {
// TODO Auto-generated method stub
System.out.println("Preparing Coffee");
getBeverage().setBeverageName("Coffee");
}
@Override
void setWater() {
// TODO Auto-generated method stub
System.out.println("Adding 15 ml of water");
getBeverage().setWater(15);
}
@Override
void setMilk() {
// TODO Auto-generated method stub
System.out.println("Adding 30 ml of milk");
getBeverage().setMilk(30);
}
@Override
void setSugar() {
// TODO Auto-generated method stub
System.out.println("Adding 50 ml of milk");
getBeverage().setSugar(50);
}
@Override
void setPowderQuantity() {
// TODO Auto-generated method stub
System.out.println("Adding 2 tbps of coffe powder");
getBeverage().setPowderQuantity(2);
}
@Override
Beverage createBeverage() {
// TODO Auto-generated method stub
return new Beverage();
}
}
package coffee_builder;
public abstract class BeverageBuilder {
private Beverage beverage;
public Beverage getBeverage() {
return beverage;
}
public void setBeverage(Beverage beverage) {
this.beverage = beverage;
}
public final Beverage buildBeverage()
{
beverage = createBeverage();
setBeverage(beverage);
setBeverageType();
setWater();
setMilk();
setSugar();
setPowderQuantity();
System.out.println(beverage.getBeverageName() + "Delivered");
return beverage;
}
abstract void setBeverageType();
abstract void setWater();
abstract void setMilk();
abstract void setSugar();
abstract void setPowderQuantity();
abstract Beverage createBeverage();
}
package coffee_builder;
public class Beverage {
private int water;
private int sugar;
private int milk;
private int powderQuantity;
private String beverageName;
public int getWater() {
return water;
}
public void setWater(int water) {
this.water = water;
}
public int getSugar() {
return sugar;
}
public void setSugar(int sugar) {
this.sugar = sugar;
}
public int getPowderQuantity() {
return powderQuantity;
}
public void setPowderQuantity(int powderQuantity) {
this.powderQuantity = powderQuantity;
}
public String getBeverageName() {
return beverageName;
}
public void setBeverageName(String beverageName) {
this.beverageName = beverageName;
}
public int getMilk() {
return milk;
}
public void setMilk(int milk) {
this.milk = milk;
}
}
No comments:
Post a Comment