1 / 22

Builder Pattern

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.

sara-bishop
Download Presentation

Builder Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Builder Pattern By James Sheets

  2. 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.

  3. 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

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

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

  6. 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

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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Building Process

  14. 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

  15. 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; } }

  16. 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(); }

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

  18. 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(); } }

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

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

  21. Pattern Benefits

  22. The End

More Related