by james sheets. an object creational pattern. separates the construction of a complex object from...

22
By James Sheets

Upload: ursula-patrick

Post on 04-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

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

By James Sheets

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

An object creational pattern.

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

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

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

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

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.

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Director

Constructor()

Builder

BuildPart() : void

ConcreteBuilder

BuildPart() : void

GetResult() : Product

ProductFor all objects in builder { builder->BuildPart();}

For all objects in builder { builder->BuildPart();}

builder

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

Waiter

Waiter(PizzaBuilder)GetPizza() : PizzaConstructPizza() : void

PizzaBuilder

GetPizza() : PizzaBuildDough() : voidBuildSauce() : voidBuildToppings() : void

HawaiianPizzaBuilder

BuildDough() : voidBuildSauce() : voidBuildToppings() : void

SpicyPizzaBuilder

BuildDough() : voidBuildSauce() : voidBuildToppings() : void

Pizza

SetDough(String) : voidSetSauce(String) : voidSetToppings(String) : void

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

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

}

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

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

}

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

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

}

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

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

} }

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

/** A customer ordering a pizza. */ class BuilderExample {

public static void main(String[] args) {

// ConcreteBuilder cast as a BuilderPizzaBuilder hawaiian= new HawaiianPizzaBuilder(); // DirectorWaiter waiter = new Waiter(hawaiian);

waiter.constructPizza(); // ProductPizza pizza = waiter.getPizza();

} }

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

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

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