410 likes | 766 Views
Builder Pattern. By James Sheets. Pattern Definition. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process can create different representations. When To Use The Builder Pattern.
E N D
Builder Pattern By James Sheets
Pattern Definition An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process can create different representations.
When To Use The Builder Pattern An object requires different representations in different contexts How a complex object is constructed is important You need to encapsulate the logic behind constructing a complex object
Example Scenarios Convert an internal object into various file formats (pdf, doc) Building a maze. Create a map, start point, end point, and then the maze walls.
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product Product: the object being built
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product Builder: determines what gets built
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product • Builder: determines what gets built • A different concrete builder for every variation of the product
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product • Builder: determines what gets built • A different concrete builder for every variation of the product • Concrete builder provides an interface to retrieve the product
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product Director: determines how it’s built
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product • Director: determines how it’s built • Constructor takes a Builder object
Class Diagram ConcreteBuilder BuildPart() : void GetResult() : Product Builder BuildPart() : void Director Constructor() builder For all objects in builder { builder->BuildPart(); } Product • Director: determines how it’s built • Constructor takes a builder object • Creates an object through the builder’s interface
Pattern In Use: Class Model Waiter Waiter(PizzaBuilder) GetPizza() : Pizza ConstructPizza() : void PizzaBuilder GetPizza() : Pizza BuildDough() : void BuildSauce() : void BuildToppings() : void Pizza SetDough(String) : void SetSauce(String) : void SetToppings(String) : void HawaiianPizzaBuilder BuildDough() : void BuildSauce() : void BuildToppings() : void SpicyPizzaBuilder BuildDough() : void BuildSauce() : void BuildToppings() : void
Pattern in use: Code /** "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; } }
Pattern in use: Code /** "Abstract Builder" */ abstract class PizzaBuilder { protected Pizza pizza; public void PizzaBuilder() { pizza = new Pizza(); } public Pizza getPizza() { return pizza; } public abstract void buildDough(); public abstract void buildSauce(); public abstract void buildTopping(); }
Pattern in use: Code /** "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"); } }
Pattern in use: Code /** "Director" */ class Waiter { private PizzaBuilder pizzaBuilder; public Waiter(PizzaBuilder pb) { pizzaBuilder = pb; } public Pizza getPizza() { return pizzaBuilder.getPizza(); } public void constructPizza() { pizzaBuilder.buildDough(); pizzaBuilder.buildSauce(); pizzaBuilder.buildTopping(); } }
Pattern in use: Code /** A customer ordering a pizza. */ class BuilderExample { public static void main(String[] args) { // ConcreteBuilder cast as a Builder PizzaBuilder hawaiian= new HawaiianPizzaBuilder(); // Director Waiter waiter = new Waiter(hawaiian); waiter.constructPizza(); // Product Pizza pizza = waiter.getPizza(); } }
Builder Vs. Abstract Factory Concrete Builders create families of objects, like an Abstract Factory Abstract Factory returns the family of related objects Builders construct a complex object one step at a time