/** "Product" */ class Pizza { private String dough = ""; private String sauce = ""; private String topping = ""; public void setDough (String dough) { this.dough = dough; } public void setSauce (String sauce) { this.sauce = sauce; } public void setTopping (String topping) { this.topping = topping; } } /** "Abstract Builder" */ abstract class PizzaBuilder { protected Pizza pizza; public Pizza getPizza() { return pizza; } public void createNewPizzaProduct() { pizza = new Pizza(); } public abstract void buildDough(); public abstract void buildSauce(); public abstract void buildTopping(); } /** "ConcreteBuilder" */ class HawaiianPizzaBuilder extends PizzaBuilder { public void buildDough() { pizza.setDough("cross"); } public void buildSauce() { pizza.setSauce("mild"); } public void buildTopping() { pizza.setTopping("ham+pineapple"); } } /** "ConcreteBuilder" */ class SpicyPizzaBuilder extends PizzaBuilder { public void buildDough() { pizza.setDough("pan baked"); } public void buildSauce() { pizza.setSauce("hot"); } public void buildTopping() { pizza.setTopping("pepperoni+salami"); } } /** "Director" */ class Waiter { private PizzaBuilder pizzaBuilder; public void setPizzaBuilder (PizzaBuilder pb) { pizzaBuilder = pb; } public Pizza getPizza() { return pizzaBuilder.getPizza(); } public void constructPizza() { pizzaBuilder.createNewPizzaProduct(); pizzaBuilder.buildDough(); pizzaBuilder.buildSauce(); pizzaBuilder.buildTopping(); } } /** A customer ordering a pizza. */ class BuilderExample { public static void main(String[] args) { Waiter waiter = new Waiter(); PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder(); PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder(); waiter.setPizzaBuilder ( hawaiianPizzaBuilder ); waiter.constructPizza(); Pizza pizza = waiter.getPizza(); } }
// PlanComponent package nijel.product; public class PlanComponent { protected String name; protected String description; public PlanComponent(String name, String description) { this.name = name; this.description = description; } public void add(PlanComponent planComponent) { throw new UnsupportedOperationException(); } public void remove(PlanComponent planComponent) { throw new UnsupportedOperationException(); } public PlanComponent getChild(int index) { throw new UnsupportedOperationException(); } public String getName() { throw new UnsupportedOperationException(); } public String getDescription() { throw new UnsupportedOperationException(); } public void print() { throw new UnsupportedOperationException(); } } // Plan package nijel.product; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Plan extends PlanComponent { private List<PlanComponent> planComponents; public String getName() { return name; } public String getDescription() { return description; } public Plan(String name, String description) { super(name, description); planComponents = new ArrayList<PlanComponent>(); } public void add(PlanComponent planComponent) { planComponents.add(planComponent); } public PlanComponent getChild(int index) { return planComponents.get(index); } public void remove(PlanComponent planComponent) { planComponents.remove(planComponent); } public void print() { System.out.println("\n" + getName()); System.out.println(", " + getDescription()); System.out.println("------------------------------"); Iterator iterator = planComponents.iterator(); while (iterator.hasNext()) { PlanComponent planComponent = (PlanComponent) iterator.next(); planComponent.print(); } } } // PlanItem package nijel.product; public class PlanItem extends PlanComponent { public PlanItem(String name, String description) { super(name, description); } public String getName() { return name; } public String getDescription() { return description; } public void print() { System.out.println("\t" + getName() + "\n"); System.out.println("\t\t" + getDescription() + "\n"); } }
// director package nijel.builder; import nijel.product.PlanComponent; public abstract class Planner { abstract public PlanComponent makePlan(); } // concrete director package nijel.builder; import nijel.product.PlanComponent; public class MyPlanner extends Planner { private Builder builder; public MyPlanner(Builder builder) { this.builder = builder; } public PlanComponent makePlan() { int first, second, third; first = 1; builder.buildDay(first); builder.addHotel(first, "Patternsland 5 star Hotel"); builder.addTickets(first, "Park Ticket: R"); builder.addReservation(first, "Dinner at VERY expensive French restaurant"); second = 2; builder.buildDay(second); builder.addHotel(second, "Maybe Hotel"); builder.addTickets(second, "Park Ticket: A"); builder.addSpecialEvent(second, "Patterns on Ice"); builder.addReservation(second, "just dinner"); third = 3; builder.buildDay(third); builder.addHotel(third, "Wannabe Hotel"); builder.addTickets(third, "Ticket for Children!"); return builder.getVacationPlan(); } } // builder package nijel.builder; import nijel.product.PlanComponent; public interface Builder { abstract public void buildDay(int date); abstract public void addHotel(int date, String hotel); abstract public void addTickets(int date, String ticket); abstract public void addSpecialEvent(int date, String event); abstract public void addReservation(int date, String restaurant); abstract public PlanComponent getVacationPlan(); } // concrete builder package nijel.builder; import nijel.product.Plan; import nijel.product.PlanComponent; import nijel.product.PlanItem; public class VacationBuilder implements Builder { private PlanComponent plan; public VacationBuilder(String description) { plan = new Plan(description); } public void addHotel(int date, String hotel) { PlanComponent hotelReservation = new PlanItem(hotel); plan.getChild(date - 1).add(hotelReservation); } public void addSpecialEvent(int date, String event) { PlanComponent specialEvent = new Plan("Special Event"); specialEvent.add(new PlanItem(event)); plan.getChild(date - 1).add(specialEvent); } public void addTickets(int date, String ticket) { PlanComponent admissionTicket = new PlanItem(ticket); plan.getChild(date - 1).add(admissionTicket); } public void buildDay(int date) { plan.add(new Plan(date + "day's plan")); } public PlanComponent getVacationPlan() { return plan; } public void addReservation(int date, String restaurant) { PlanComponent reservation = new Plan("Dining"); reservation.add(new PlanItem(restaurant)); plan.getChild(date - 1).add(reservation); } }
package nijel.client; import nijel.builder.*; import nijel.product.*; public class Client { public static void main(String[] args) { PlanComponent perfectVacationPlan; Planner perfectPlanner = new MyPlanner(new VacationBuilder("Perfect Vacation Plan")); perfectVacationPlan = perfectPlanner.makePlan(); perfectVacationPlan.print(); } }