builder pattern n.
Download
Skip this Video
Download Presentation
Builder Pattern

Loading in 2 Seconds...

play fullscreen
1 / 22

Builder Pattern - PowerPoint PPT Presentation


  • 88 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Builder Pattern' - sara-bishop


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
builder pattern

Builder Pattern

By James Sheets

pattern definition
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
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
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
Class Diagram

ConcreteBuilder

BuildPart() : void

GetResult() : Product

Builder

BuildPart() : void

Director

Constructor()

builder

For all objects in builder {

builder->BuildPart();

}

Product

class diagram1
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 diagram2
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 diagram3
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 diagram4
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 diagram5
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 diagram6
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 diagram7
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
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
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 code1
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 code2
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 code3
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 code4
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
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

ad