Presentation is loading. Please wait.

Presentation is loading. Please wait.

By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.

Similar presentations


Presentation on theme: "By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process."— Presentation transcript:

1 By James Sheets

2 An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process can create different representations.

3 1. An object requires different representations in different contexts 2. How a complex object is constructed is important 3. You need to encapsulate the logic behind constructing a complex object

4  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.

5 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder

6 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Product: the object being built

7 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Builder: determines what gets built

8 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Builder: determines what gets built A different concrete builder for every variation of the product

9 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Builder: determines what gets built A different concrete builder for every variation of the product Concrete builder provides an interface to retrieve the product

10 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Director: determines how it’s built

11 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Director: determines how it’s built Constructor takes a Builder object

12 Director Constructor() Builder BuildPart() : void ConcreteBuilder BuildPart() : void GetResult() : Product Product For all objects in builder { builder->BuildPart(); } For all objects in builder { builder->BuildPart(); } builder Director: determines how it’s built Constructor takes a builder object Creates an object through the builder’s interface

13 Determine which Concrete Builder you need Create a Director, passing the Builder into it’s constructor Director call build methods in Builder Retrieve the built Product from the Builder

14 Waiter Waiter(PizzaBuilder) GetPizza() : Pizza ConstructPizza() : void PizzaBuilder GetPizza() : Pizza BuildDough() : void BuildSauce() : void BuildToppings() : void HawaiianPizzaBuilder BuildDough() : void BuildSauce() : void BuildToppings() : void SpicyPizzaBuilder BuildDough() : void BuildSauce() : void BuildToppings() : void Pizza SetDough(String) : void SetSauce(String) : void SetToppings(String) : void

15 /** "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; } }

16 /** "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(); }

17 /** "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"); } }

18 /** "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(); }

19 /** 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(); }

20  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

21 Allows variation from internal object representation and constructed forms Separates code from the construction process Gives finer control over the construction process

22


Download ppt "By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process."

Similar presentations


Ads by Google